Unit Test can't find Core Data model file

前端 未结 5 1369
無奈伤痛
無奈伤痛 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

    This method will get your bundle from any target. However, for each target you add, you have to manually add the plist bundle identifier to the identifiers array, because there is no way to get it programmatically. The advantage is that you can use the same code for testing or running the application.

    +(NSBundle*) getBundle 
    {
        NSBundle *bundle = nil;
    
        // try your manually set bundles
        NSArray *identifiers = [NSArray arrayWithObjects: @"com.your.application",
                                                          @"com.your.test",
                                                          nil];
        for(NSString *bundleId in identifiers) {
            bundle = [NSBundle bundleWithIdentifier:bundleId];
            if (bundle!=nil) break;
        }
    
        // try the main bundle
        if (bundle==nil) bundle = [NSBundle mainBundle];
    
        // abort
        assert(bundle!=nil && "Missing bundle. Check the Bundle identifier on 
               the plist of this target vs the identifiers array in this class.");
    
        return bundle;
    }
    

提交回复
热议问题