Question

I have two specs written in Kiwi, both of which call these two methods in beforeAll:

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

And [MagicalRecord cleanUp]; in afterAll.

One of these specs fails saying Default Context is nil! Did you forget to initialize the Core Data Stack? but the other does not. When I comment out the first spec, the second spec still fails. When I comment out the second spec, the first spec still passes, so it doesn't seem like the order matters or they're being run in parallel and causing an issue.

Can anyone shine some light on why this could be happening? Here is the full spec that is failing:

#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

It fails in + (NSManagedObjectContext *) MR_defaultContext with the NSAssert saying Default Context is nil! Did you forget to initialize the Core Data Stack?.

Was it helpful?

Solution

Stupid mistake! This line:

context(@"should pass", ^{

should have been this:

it(@"should pass", ^{

This was causing the beforeAll block not to run so the Core Data stack was not being configured properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top