【问题标题】:Default context is nil when testing MagicalRecord objects with Kiwi使用 Kiwi 测试 MagicalRecord 对象时,默认上下文为 nil
【发布时间】:2013-11-18 16:39:47
【问题描述】:

我有两个用 Kiwi 编写的规范,它们都在 beforeAll 中调用这两个方法:

[MagicalRecord setDefaultModelFromClass:[self class]];
[MagicalRecord setupCoreDataStackWithInMemoryStore];

还有[MagicalRecord cleanUp];afterAll

其中一个规范未能说出Default Context is nil! Did you forget to initialize the Core Data Stack?,但另一个没有。当我注释掉第一个规范时,第二个规范仍然失败。当我注释掉第二个规范时,第一个规范仍然通过,所以看起来顺序并不重要,或者它们正在并行运行并导致问题。

谁能阐明为什么会发生这种情况?以下是失败的完整规范:

#import "Kiwi.h"
#import "AuthenticationHelper.h"
#import "Admin.h"

SPEC_BEGIN(AuthenticationManagerSpec)

describe(@"The authentication helper", ^{

    beforeAll(^{
        [MagicalRecord setDefaultModelFromClass:[self class]];
        [MagicalRecord setupCoreDataStackWithInMemoryStore];
    });

    afterAll(^{
        [MagicalRecord cleanUp];
    });

    context(@"when given correct credentials", ^{
        context(@"should pass", ^{
            NSString *email = @"foo@bar.com";
            NSString *password = @"test_password_1234";

            Admin *admin = [Admin createEntity];
            admin.email = email;
            admin.password = password;

            __block BOOL loggedInSuccessfully = NO;
            [AuthenticationHelper authenticateUserWithEmail:email
                                                      password:password
                                                    completion:^(BOOL success,
                                                                 id response) {
             loggedInSuccessfully = success;
             }];

            [admin deleteEntity];

            [[expectFutureValue(theValue(loggedInSuccessfully)) shouldEventually] beTrue];

        });
    });

});

SPEC_END

它在 + (NSManagedObjectContext *) MR_defaultContext 中失败,NSAssert 说 Default Context is nil! Did you forget to initialize the Core Data Stack?

【问题讨论】:

    标签: ios objective-c magicalrecord kiwi


    【解决方案1】:

    愚蠢的错误!这一行:

    context(@"should pass", ^{

    应该是这样的:

    it(@"should pass", ^{

    这导致beforeAll 块无法运行,因此核心数据堆栈未正确配置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 2019-03-20
      相关资源
      最近更新 更多