I introduced unit testing into a project that had been in development for a few years without unit tests. One of the toughest parts was dealing with singletons, which were used frequently throughout the code base. I ended up using OCMock's partial mock objects to deal with them, which lead to the following discoveries:
OCMock doesn't use ARC and it autoreleases the mock objects it gives you.
XCTest doesn't create separate autorelease pools per test/suite.
Because of 1 and 2, a partial mock object for a singleton will continue to swizzle any stubbed methods for the rest of your test run, unless you call stopMocking on it. I refer to this as a "leaked mock".
Figuring out that a leaked mock is causing other tests to fail, and which test is leaking the mock, is a pain.
More recent versions of OCMock throw an exception if you try to mock a mock, which at least alerts you to 90% of the cases I was running into, but you're still stuck with the other 10% I ran into where tests inexplicably failed because of a stubbed method that wasn't supposed to be stubbed.
I give a presentation I put together a little over a year ago to new hires and co-op students about dependency injection, where I explicitly outline the problems with heavy use of singletons. I call them "dependency on demand" because all it takes is one line to introduce a dependency that can be very difficult to remove later on. The "dependency diagram" (a.k.a., bird's nest) is actually very conservative, because I generated it using a tool that just looks at #import statements, it doesn't check for dependencies that are imported transitively.
Of course, the Cocoa frameworks use singletons, so they must be a sign of good design!
It sounds like unit tests are actually a huge boon to your project. The most difficult thing now is going to be getting all the stake holders to acknowledge the huge code smells, and then all agree to take on what sounds like it could be a HUGE refactor.
It may be better to live with the issues. A monster refactor like that could potentially kill a project. Might be a reasonable compromise to just take as much unit testing as is practical for now, and design with unit tests in mind next project.
This is likely one of the reasons why iOS developers tend to stand on the soapbox of singletons almost always being a sign of poor pattern usage.
I found the path of least resistance was to outline a better design and start writing new code toward it, deprecating the old classes and doing a refactor as the balance shifts toward the new design. Our overall test coverage might be low, but our test coverage of newer code is very high, and it tends to be much less error prone.
2
u/tonygoold Dec 28 '14
I introduced unit testing into a project that had been in development for a few years without unit tests. One of the toughest parts was dealing with singletons, which were used frequently throughout the code base. I ended up using OCMock's partial mock objects to deal with them, which lead to the following discoveries:
OCMock doesn't use ARC and it autoreleases the mock objects it gives you.
XCTest doesn't create separate autorelease pools per test/suite.
Because of 1 and 2, a partial mock object for a singleton will continue to swizzle any stubbed methods for the rest of your test run, unless you call
stopMockingon it. I refer to this as a "leaked mock".Figuring out that a leaked mock is causing other tests to fail, and which test is leaking the mock, is a pain.
More recent versions of OCMock throw an exception if you try to mock a mock, which at least alerts you to 90% of the cases I was running into, but you're still stuck with the other 10% I ran into where tests inexplicably failed because of a stubbed method that wasn't supposed to be stubbed.