Where does OCUnit store NSUserDefaults?

陌路散爱 提交于 2019-12-24 07:09:47

问题


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?


回答1:


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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/6193597/where-does-ocunit-store-nsuserdefaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!