如何告诉MagicalRecord不使用基于文件的Core Data而是使用内存设置?

时间:2022-01-27 19:36:38

I followed this great article to get into Unit Testing regarding Core Data. The setup seems simple and involves just some view lines of code.

我按照这篇伟大的文章进入了关于核心数据的单元测试。设置看起来很简单,只涉及一些视图行代码。

- (void)setUp;
{
    [MagicalRecord setDefaultModelWithClass:[self class]];
    [MagicalRecord setupCoreDataStackWithInMemoryStore];
}

- (void)tearDown;
{
    [MagicalRecord cleanUp];
}

- (void)testSomeCalculationOnMyEntity;
{
    NSNumber *count = [MyEntity MR_numberOfEntities];
    // STAssert([testEntity customCalculation] == expectedValue, @"expected a good calculation");
}

@end

The problem is, that each time I for example check for the amount of entities in the in memory set up of Core Data by calling [MyEntity MR_numberOfEntities] (like above), I get the amount of objects, which are stored in the file based setup which are a couple of thousand objects. How does this happen? I mean the second line in setUp indicates the in memory one, doesn't it? And this case should return 0 as the amount of objects stored.

问题是,每次我通过调用[MyEntity MR_numberOfEntities](如上所述)检查内核设置的核心数据中的实体数量,我得到的对象数量,存储在基于文件的设置是几千个对象。这是怎么发生的?我的意思是setUp中的第二行表示内存中的第二行,不是吗?并且这种情况应该作为存储的对象量返回0。

Thanks for any suggestions!

谢谢你的任何建议!

Edit:

@casademora put me on the right track. The following works setup works fine for me now.

@casademora让我走上正轨。以下工作设置现在对我来说很好。

- (void)setUp;
{
    [MagicalRecord cleanUp]; // This solved the mystery.

    // I don't now why I had to remove this line, though.
    // [MagicalRecord setDefaultModelWithClass:[self class]];

    [MagicalRecord setupCoreDataStackWithInMemoryStore];
}

- (void)tearDown;
{
    [MagicalRecord cleanUp];
}

- (void)testSomeCalculationOnMyEntity;
{
    NSNumber *count = [MyEntity MR_numberOfEntities];
    // STAssert([testEntity customCalculation] == expectedValue, @"expected a good calculation");
}

@end

2 个解决方案

#1


1  

The method used to setup Core Data here should not ever load a file based store. If you step into it using the debugger, you should see that it initializes a persistent store coordinator using the NSInMemoryStore type...eventually.

此处用于设置Core Data的方法不应该加载基于文件的存储。如果您使用调试器进入它,您应该看到它使用NSInMemoryStore类型初始化持久性存储协调器...最终。

My guess as to why this happens could be because you did not add a cleanUp call to a previous test case, causing a previous core data stack, or persistent store, to hang around into this test.

我猜测为什么会发生这种情况可能是因为你没有在之前的测试用例中添加一个cleanUp调用,导致之前的核心数据堆栈或持久存储在这个测试中徘徊。

This is the exact code setup I use to unit test core data all the time, and it has never loaded an unintended persistent store for me. When you debug into the method, make sure to run this command:

这是我用来单独测试核心数据的确切代码设置,它从来没有为我加载一个非预期的持久存储。调试方法时,请确保运行此命令:

po [self persistentStores]

When you get to the loading of the persistent store coordinator. If there are existing stores, this will at least verify that there are some pre-existing stores hanging around.

当你开始加载持久性商店协调员。如果有现有商店,这至少会验证是否存在一些预先存在的商店。

#2


1  

You could also add the following which is performed once on class initialisation. This worked for me.

您还可以添加在类初始化时执行一次的以下内容。这对我有用。

 +(void)setUp
   {
           [MagicalRecord cleanUp];
   }

#1


1  

The method used to setup Core Data here should not ever load a file based store. If you step into it using the debugger, you should see that it initializes a persistent store coordinator using the NSInMemoryStore type...eventually.

此处用于设置Core Data的方法不应该加载基于文件的存储。如果您使用调试器进入它,您应该看到它使用NSInMemoryStore类型初始化持久性存储协调器...最终。

My guess as to why this happens could be because you did not add a cleanUp call to a previous test case, causing a previous core data stack, or persistent store, to hang around into this test.

我猜测为什么会发生这种情况可能是因为你没有在之前的测试用例中添加一个cleanUp调用,导致之前的核心数据堆栈或持久存储在这个测试中徘徊。

This is the exact code setup I use to unit test core data all the time, and it has never loaded an unintended persistent store for me. When you debug into the method, make sure to run this command:

这是我用来单独测试核心数据的确切代码设置,它从来没有为我加载一个非预期的持久存储。调试方法时,请确保运行此命令:

po [self persistentStores]

When you get to the loading of the persistent store coordinator. If there are existing stores, this will at least verify that there are some pre-existing stores hanging around.

当你开始加载持久性商店协调员。如果有现有商店,这至少会验证是否存在一些预先存在的商店。

#2


1  

You could also add the following which is performed once on class initialisation. This worked for me.

您还可以添加在类初始化时执行一次的以下内容。这对我有用。

 +(void)setUp
   {
           [MagicalRecord cleanUp];
   }