这是我的测试用例
NSError *error = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:[TestPerson entityName]];
request.predicate = [NSPredicate predicateWithFormat:@"firstName = \"mark\""];
NSArray *results = [_controller.mainContext executeFetchRequest:request error:&error];
TestPerson *mark1 = results.firstObject;
XCTAssertNil(error);
XCTAssertEqual(1, results.count);
XCTAssertTrue([@"mark" isEqualToString:mark1.firstName]);
results = [[_controller backgroundContext] executeFetchRequest:request error:&error];
TestPerson *mark2 = results.firstObject;
XCTAssertNil(error);
XCTAssertEqual(1, results.count);
XCTAssertTrue([@"mark" isEqualToString:mark2.firstName]);
XCTAssertTrue([mark1.objectID isEqual:mark2.objectID]);
我的堆栈看起来像这样
PSC--+--privateContexts--mainContext
|
+------------backgroundContexts
两个firstNames评估标记。 setUp和tearDown代码很简单。设置:
NSError *error = nil;
_controller = [CoreDataController new];
_controller.model = [CoreDataTests model];
_controller.storeType = NSInMemoryStoreType;
_controller.storeConfiguration = nil;
_controller.storeURL = nil;
_controller.storeOptions = nil;
[_controller createCoreDataStack];
NSManagedObjectContext *context = _controller.mainContext;
TestPerson *mark = [NSEntityDescription insertNewObjectForEntityForName:[TestPerson entityName] inManagedObjectContext:context];
mark.firstName = @"mark";
mark.lastName = @"camper";
[context save:&error];
TestPerson *mary = [NSEntityDescription insertNewObjectForEntityForName:[TestPerson entityName] inManagedObjectContext:context];
mary.firstName = @"mary";
mary.lastName = @"camper";
[context save:&error];
XCTAssert(error == nil);
TEARDOWN:
_controller = nil;
mark1的描述:
<TestPerson: 0x7fd212f48c10> (entity: TestPerson; id: 0x7fd212f46700 <x-coredata:///TestPerson/t9DB3981E-74CE-42CE-B054-9E5C280BEDC72> ; data: {
firstName = mark;
lastName = camper;
})
MARK2:
<TestPerson: 0x7fd212f1d930> (entity: TestPerson; id: 0x7fd212f3dc40 <x-coredata://D9092214-6EC3-4918-9360-4663ABC56E37/TestPerson/p1> ; data: {
firstName = mark;
lastName = camper;
})
断言失败是XCTAssertTrue([mark1.objectID isEqual:mark2.objectID]);
我是否有一些巧妙的遗漏?