0

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?

foFox
  • 1,124
  • 1
  • 14
  • 24

2 Answers2

0

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.

casademora
  • 67,775
  • 17
  • 69
  • 78
  • That method either creates a new one or returns a fetched object. There is a need to test that, as I want to confirm that the first time the object is seen, it will be created, and nil will not be returned. Hence MR_createInContext will be fetched. BTW I found the error, this method is defined on an NSManagedObject not the context. – foFox May 02 '13 at 21:29
  • MR_createInContext will be called* – foFox May 02 '13 at 21:35
0

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

foFox
  • 1,124
  • 1
  • 14
  • 24