xcode 单元测试找不到核心数据模型文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5662947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:49:51  来源:igfitidea点击:

Unit Test can't find Core Data model file

iphonecocoaxcodeunit-testingocunit

提问by Luther Baker

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

我创建了一个包含 Core Data 模型的项目。该应用程序查找模型文件 (.momd) 并运行得很好。

Unfortunately, the unit test keeps returning null:

不幸的是,单元测试不断返回 null:

NSURL *dataModelURL = [[NSBundle mainBundle] URLForResource:@"myDataModel" withExtension:@"momd"];

I can see the myDataModel.xdatamodeld folder and file in BOTH the main target and the unit testing target's Compile Sources directory - but that doesn't seem to be enough. What else am I missing in the unit test target?

我可以在主目标和单元测试目标的编译源目录中看到 myDataModel.xdatamodeld 文件夹和文件 - 但这似乎还不够。我在单元测试目标中还缺少什么?

Thanks, -Luther

谢谢,-路德

回答by Till

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.

不幸的是,单元测试目标不使用应用程序的主包,但它创建了一个特殊的 UnitTest 包。因此,如果您需要在测试中使用捆绑资源(如 Core Data 模型),则需要解决该问题。

The most simple and most flexible workaround would be using the bundleForClass:method of NSBundlewithin 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.

最简单和最灵活的解决方法是使用测试代码中的bundleForClass:方法NSBundle。该方法的参数可以简单地[self class]在您的测试中给出。这样您就可以重用此代码,而无需在多个项目中调整包标识符。

Example:

例子:

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

回答by Luther Baker

The answer has to do with the bundle. A unit test target doesn't use the 'main' bundle. It creates its own bundle which, in my case, defaulted to 'com.yourcompany.UnitTest' - straight out of the [Target]-info.plist.

答案与捆绑包有关。单元测试目标不使用“主要”包。它创建了自己的包,在我的例子中,它默认为“com.yourcompany.UnitTest”——直接来自 [Target]-info.plist。

The corrected solution then looks like this:

更正后的解决方案如下所示:

NSBundle *bundle = [NSBundle bundleWithIdentifier:@"com.yourcompany.UnitTests"];
NSURL *url = [bundle URLForResource:@"myDataModel" withExtension:@"momd"];

Thanks

谢谢

回答by user966697

Had a similar problem, i solved it using the OCMock framework, so i did not need to change the application code

有一个类似的问题,我使用OCMock框架解决了它,所以我不需要更改应用程序代码

@interface TestCase()
    @property (nonatomic, strong) id bundleMock;
@end

@implementation TestCase

- (void)setUp
{
    self.bundleMock = [OCMockObject mockForClass:[NSBundle class]];
    [[[self.bundleMock stub] andReturn:[NSBundle bundleForClass:[self class]]] mainBundle];
    [super setUp];
}

- (void)tearDown
{
    [self.bundleMock stopMocking];
    [super tearDown];
}

回答by Jano

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 identifiersarray, 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.

此方法将从任何目标获取您的包。但是,对于您添加的每个目标,您必须手动将 plist 包标识符添加到identifiers数组中,因为无法以编程方式获取它。优点是您可以使用相同的代码来测试或运行应用程序。

+(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;
}

回答by Kevin Delord

My problem was indeed the wrong Bundle! As I was trying to use a database from/within a Framework I 'simply' has to load the db from the corresponding Bundle!

我的问题确实是错误的捆绑包!当我试图从/在框架内使用数据库时,我“只是”必须从相应的Bundle!

Here is some code in Swift4 using MagicalRecord:

这是 Swift4 中使用的一些代码MagicalRecord

// Load the bundle
let frameworkBundle = Bundle(for: AClassFromTheFramework.self)
let managedObjectModel = NSManagedObjectModel.mergedModel(from: [frameworkBundle])
// Use the new `managedObjectModel` by default
MagicalRecord.setShouldAutoCreateManagedObjectModel(false)
NSManagedObjectModel.mr_setDefaultManagedObjectModel(managedObjectModel)
// Load the database 
MagicalRecord.setupCoreDataStack(withAutoMigratingSqliteStoreNamed: "db.sqlite")

And voilà!

瞧!