开发者

Where does OCUnit store NSUserDefaults?

I read 开发者_JAVA技巧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?


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.)


In my environment (Xcode 4.2, running tests in the simulator) NSUserDefaults writes to the following file:

~/Library/Application Support/iPhone Simulator/Library/Preferences/otest.plist`

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 the NSUserDefault instance method synchronize while the tests are running.


It seems that OCUnit doesn't store NSUserDefaults persistently but rather in memory. But, I could be wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜