使用Kiwi测试MagicalRecord对象时,默认上下文为nil

时间:2013-11-18 16:39:48

标签: ios objective-c magicalrecord kiwi

我有两个用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?

1 个答案:

答案 0 :(得分:0)

愚蠢的错误!这一行:

context(@"should pass", ^{

应该是这样的:

it(@"should pass", ^{

这导致beforeAll块无法运行,因此未正确配置Core Data堆栈。