Where does OCUnit store NSUserDefaults?
Asked Answered
G

3

1

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?

Gilud answered 31/5, 2011 at 21:10 Comment(0)
G
1

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

Gilud answered 31/5, 2011 at 21:12 Comment(0)
A
5

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

Adage answered 3/6, 2011 at 5:58 Comment(1)
Are you sure? I'm using XCode 4, but it seems that, with OCUnit, NSUserDefaults aren't being stored to the application's .plist file where NSUserDefaults are normally stored. Actually, I don't see them being stored at all. Although, I could be missing something.Gilud
P
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.

Piacular answered 5/2, 2012 at 23:18 Comment(0)
G
1

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

Gilud answered 31/5, 2011 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.