kender
kender

Reputation: 87171

How to access NSDocumentDirectory in unit tests (OCUnit)

I'm trying to run my app in OCUnit environment. It uses a sqlite database and I'm having it in the app's documents folder.

However when running from the tests, the path for the database is set to:

~/Library/Application Support/iPhone Simulator/6.0/Documents/Test.sqlite

While the same code gives me a path of

~/Library/Application Support/iPhone Simulator/6.0/Applications/34536EFB-005A-44A1-AD99-07D6B9F6888B/Documents/Test.sqlite

when I run it on the simulator.

How can I use NSDocumentDirectory in my tests and get what I need?

Upvotes: 2

Views: 1285

Answers (1)

iska
iska

Reputation: 2248

The application target is not the same as the test target, that's why you get different paths.

When using OCUnit you can get the test NSBundle like this:

NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"myResource" ofType:@"someType"];

You have to add the database to the resources of the test target and use it accordingly.

Or you can build the path like this (taken from this OS Answer):

// Test case -> path inside "UnitTests" folder in the project directory
NSString *directory = [[NSFileManager defaultManager] currentDirectoryPath];
NSString *path = [directory stringByAppendingPathComponent:@"UnitTests/"];
path = [path stringByAppendingPathComponent:@"Test.sqlite"];

Upvotes: 2

Related Questions