views:

31

answers:

2

Hello,

I am trying to get started with unit testing an app that uses Core Data. In the setUp method of my unit first test, I can get the path to my data model but for some reason cannot convert it to a NSURL.

My setUp method is:

- (void)setUp {

    NSBundle *bundle = [NSBundle bundleWithIdentifier:@"com.testcompany.LogicTests"];
    STAssertNotNil(bundle, @"Error finding bundle to create Core Data stack.");

    NSString *path = [bundle pathForResource:@"DataModel" ofType:@"momd"];
    STAssertNotNil(path, @"The path to the resource cannot be nil.");

    NSURL *modelURL = [NSURL URLWithString:path];
    STAssertNotNil(modelURL, @"The URL to the resource cannot be nil. (tried to use path:%@, modelURL is %@)", path, modelURL);

    ...

}

The error I'm getting is:

/Users/neall/iPhone Apps/TestApp/UnitLogicTests.m:24:0 "((modelURL) != nil)" should be true. The URL to the resource cannot be nil. (tried to use path:/Users/neall/iPhone Apps/TestApp/build/Debug-iphonesimulator/LogicTests.octest/DataModel.momd, modelURL is (null))

I've checked the filesystem and the directory /Users/neall/iPhone Apps/TestApp/build/Debug-iphonesimulator/LogicTests.octest/DataModel.momd exists.

What am I missing here?

Thanks!

+2  A: 

Try using [NSURL fileURLWithPath:path] instead to construct the url

Claus Broch
A: 

Double check that you are seeing a directory called DataModel.momd at /Users/neall/iPhone Apps/TestApp/build/Debug-iphonesimulator/LogicTests.octest/DataModel.momd.

If you added a xcdatamodel file by the Add New File... command in Xcode, you would only have one file and it would be DataModel.mom (no trailing d). If that's the case, changing the

NSString *path = [bundle pathForResource:@"DataModel" ofType:@"momd"];

to

NSString *path = [bundle pathForResource:@"DataModel" ofType:@"mom"];

will fix your immediate issue.

You want to use the fileURLWithPath: that Claus suggested as well.

If you want to do versioning of your model in the future and you currently have only a .mom file, select your DataModel.xcdatamodel file in XCode and go to Design -> Data Model -> Add Model Version. This will force the creation of the DataModel.momd directory with the DataModel.mom file in it. You can just delete the new version it adds into that directory and your original tests will work.

Tyson Tune