I read the question Easy way to see saved NSUserDefaults?
I found the .plist file for the app, but where's the one for the testing bundle?
I read the question Easy way to see saved NSUserDefaults?
I found the .plist file for the app, but where's the one for the testing bundle?
It seems that OCUnit doesn't store
NSUserDefaults
persistently but rather in memory. But, I could be wrong.In my environment (Xcode 4.2, running tests in the simulator)
NSUserDefaults
writes to the following file:I guess you didn't see the file because of
NSUserDefaults
's caching mechanism (explained in the class reference), and because your project's unit tests do not run long enough for the caching time interval to elapse.otest.plist
is guaranteed to be written, though, if something invokes theNSUserDefault
instance methodsynchronize
while the tests are running.If you use Xcode 4's integration with OCUnit, it injects the test bundle into a running application. NSUserDefaults are therefore saved in the application itself. This is problematic for unit tests: When you run the app manually, your defaults may have been overwritten by your tests.
Instead of using
[NSUserDefaults standardUserDefaults]
directly, use dependency injection, passing in user defaults as an initializer argument. Then you can pass in a test double that you can query. (An alternative to dependency injection is returning NSUserDefaults from a helper method, overriding that method to return the test double when necessary.)