I am writing unit tests in GHUnit test framework. I have a NSMutableDictionary detailsDict defined in my Application delegate.
In my application delegate I set some values in & save it.
It works fine in the app but when I try to call a method to set values on it in unit tests I am not able to set any value & dictionary remains empty.
In the test case I alloc init my appDelegate. Message a method from this object which sets a few keys but When I check for its values it remains empty.
Is there anything I dont know about unit testing or what am I missing….?
Any answers pointers will be highly appreciated.
EDIT: I got it I had to separately alloc init the detailsDic variable
like this
#ifdef UNIT_TESTING
detailsDict = [[NSMutableDictionary alloc] init];
#endif
Still open to other answers on other ways of doing this.
You can’t alloc and init a variable. You can alloc and init an object, and indeed you do need to alloc and init this NSMutableDictionary object somewhere.
The proper place to do that is somewhere that will run both for your application and for every test case. The app delegate’s
initmethod is probably a good choice.That said, I’d say that your app delegate should not exist at all in your unit tests, because your unit tests are not an application. You should split out whatever functionality you mean to test into one or more separate controllers, which you create and work with from the app delegate and from the relevant unit tests.