我有以下方法:
+(Group*)groupWithID:(NSString *)idNumber
inContext:(NSManagedObjectContext *)context
{
Group *group = nil;
if(idNumber && context)
{
NSArray *result = [Group MR_findByAttribute:@"idNumber" withValue:idNumber inContext:context];
if(!result || result.count > 1)
{
// TODO (Robert): Handle error for more than one group objects or error nil results
}
else if(result.count == 0)
{
group = [Group MR_createInContext:context];
group.idNumber = idNumber;
NSAssert(group != nil, @"Group should not be nil!");
}
else
{
group = [result lastObject];
}
}
return group;
}
我正在使用以下猕猴桃规格对其进行测试:
it(@"should create new object with new id", ^{
[[[Group class] should] receive:@selector(MR_createInContext:)];
Group *group = [Group groupWithID:@"12345"
inContext:[NSManagedObjectContext MR_contextForCurrentThread]];
[[group should] beNonNil];
[[[group idNumber] should] equal:@"12345"];
});
使用以下设置:
beforeEach(^{
[MagicalRecord setupCoreDataStackWithInMemoryStore];
[MagicalRecord setDefaultModelNamed:@"Model.mom"];
});
afterEach(^{
[MagicalRecord cleanUp];
});
问题是方法 MR_createInContext 返回一个 nil,我不知道可能是什么原因,因为在其他一些测试中,相同的代码会产生一个有效的非 nil 对象。