Unit Test can't find Core Data model file

前端 未结 5 1371
無奈伤痛
無奈伤痛 2020-12-10 02:21

I\'ve created a project with a Core Data model in it. The application looks for the model file (.momd) and runs just fine.

Unfortunately, the unit test keeps returni

5条回答
  •  孤城傲影
    2020-12-10 02:50

    Unfortunately, a unit test target does not use the application's main bundle but it creates a special UnitTest-bundle. So if you need to use bundled resources (like a Core Data model) within your tests, you need to work around that issue.

    The most simple and most flexible workaround would be using the bundleForClass: method of NSBundle within your testing code. The parameter for that method can simply be given by [self class] within your tests. That way you can reuse this code without having to adjust the bundle identifiers in multiple projects.

    Example:

    - (void)testBundleLocation
    {
        NSBundle *bundle = [NSBundle bundleForClass:[self class]];
        NSURL *url = [bundle URLForResource:@"myDataModel" withExtension:@"momd"];
        ...
    }
    

提交回复
热议问题