Question

I am using Kiwi framework to test the interaction between my code and Core Data through Magical Record library. Magical record defines a category on NSManagedObject, which adds few nice methods, such as MR_createInContext:(NSManagedObjectContext *)context. I am trying to test wether or not, that method is called, and how many times.

it(@"should create new object for me object with new id", ^{
    [[[NSManagedObjectContext MR_contextForCurrentThread] should] receive:@selector(MR_createInContext:)];
    Me *me = [Me meWithID:@"12345" inContext:[NSManagedObjectContext MR_contextForCurrentThread]];
    [me shouldNotBeNil];
    [[me.idNumber should] equal:@"12345"];
});

The issue is that Kiwi does not seem to see that category despite

#import <CoreData+MagicalRecord.h>

in the test .m file.

ME_ShouldCreateNewObjectForMeObjectWithNewId (EHMeSpecs) failed: 'ME, should create new object for me object with new id' [FAILED], cannot stub -MR_createInContext: because no such method exist

How can I make Kiwi aware of the category?

Was it helpful?

Solution 2

Obviously this method is defined on an NSManagedObject not NSManagedObjectContext, it should say [[Me class] should] receive... My bad.

OTHER TIPS

Looking at the name of your test, I am guessing your intent to to verify that Core Data works, creates a new Managed Object for you and has a proper ObjectID. I don't see the need to know that Kiwi should now how to intercept the category and such. I would simply write your test like so:

it(@"should create a new object", ^{

    NSManagedObjectContext *testContext = [NSManagedObjectContext MR_context];
    id myObject = [MyEntity createWithStuff:@{...} inContext:testContext];

    assertThat(myObject, isNot(nil));
    assertThat([myObject someAttrbiute], is(equalTo(@"some value")));
});

Don't worry about testing if the category works because with this test, it's implicitly testing that anyway. If this test breaks, you'll know pretty quickly that the category isn't working.

One of my rules of testing is "Don't test the language or framework". When you're testing to see if a category works, you're violating this rule and testing something that someone else most likely has tested.

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