我正在编写一个单元测试来检查我的CoreData对象是否正在正确更新。问题是,当我尝试更新没有地址的项目,数据具有地址时,我收到验证错误。当项目已经有一个地址时,一切都按预期工作,我已经标记了添加地址并通过测试的代码
我的模型结构如下:
[Project]
-localId [int]
-serverId [int]
-name [string]
-address [Address]
[Address]
-lineOne [string]
-lineTwo [string]
-city [string]
-state [string]
-country [string]
-zip [string]
-project [Project]
项目和地址在CoreData中具有一对一的关系,并且所有属性和关系都标记为可选。我在RestKit中使用serverId和lineOne作为标识。
这是有问题的单元测试:
// Create an object to match our criteria
Project *project = [NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext];
[project setValue:@(1) forKey:@"serverId"];
//----IF I ADD THESE TWO LINES THE TEST PASSES----
//Address *address = [NSEntityDescription insertNewObjectForEntityForName:@"Address" inManagedObjectContext:[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext];
//[project setValue:address forKey:@"address"];
NSDictionary* fakeJson = @{@"id" : @1, @"name" : @"TEST", @"address" : @{@"line_1":@"123 Apple Street",@"line_2":@"Apartment 4",@"city":@"New York",@"country":@"USA",@"state":@"New York",@"zip":@"12345"}};
RKMappingTest* idTest = [RKMappingTest testForMapping:[Project showResponseMapping] sourceObject:fakeJson destinationObject:nil];
[idTest setManagedObjectContext:[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext];
[idTest performMapping];
//Assert that the destinationObject for the mapping is the existing one
XCTAssertEqualObjects(project, idTest.destinationObject, @"Expected to match the project, but did not");
这是抛出的错误:
2014-09-03 16:14:28.599 TestProject[3692:607] E restkit.object_mapping:RKMappingOperation.m:342 Validation failed while mapping attribute at key path 'address' to value <Address: 0x115b57a0> (entity: Address; id: 0x115b1e20 <x-coredata:///Address/t5016F936-C646-4CD0-84EC-366D2A8C7F056> ; data: {
city = "New York";
country = USA;
lineOne = "123 Apple Street";
lineTwo = "Apartment 4";
localId = nil;
project = nil;
state = "New York";
zip = 12345;
}). Error: The operation couldn’t be completed. (Cocoa error 1550.)
2014-09-03 16:14:28.600 Closeout[3692:607] E restkit:RKLog.m:151 Validation Error
NSLocalizedDescriptionKey: The operation couldn’t be completed. (Cocoa error 1550.)
NSValidationKeyErrorKey: address
NSValidationPredicateErrorKey: (null)
NSValidationObjectErrorKey:
<Project: 0x115a4e50> (entity: Project; id: 0x115aa980 <x-coredata:///Project/t5016F936-C646-4CD0-84EC-366D2A8C7F055> ; data: {
address = nil;
localId = nil;
name = TEST;
serverId = 1;
})
由于测试通过,如果我向初始CoreData对象添加空白地址,则问题不在于映射或地址数据无效。我还可以从日志中看出正在正确创建新的Address对象,它只是无法通过关系链接它们。
感谢您的帮助。
答案 0 :(得分:0)
很难诊断,因为我们没有您的Core Data架构定义,但抛出了NSValidationErrors,因为您有某种要求Project对象必须具有地址对象。在访问数据之前,您是否有某种必需的关系?具体来说,检查器中未选中可选复选框?见附件截图:
答案 1 :(得分:0)
经过两整天的头部刮擦解决了这个问题。问题是当您检查新项目上的“使用CoreData”框时,Xcode生成的AppDelegate中的代码。它创建了一个CoreData堆栈然后我们为单元测试创建了自己的堆栈,当RestKit向下拉数据时,它试图连接来自不同上下文的对象(即使我们在每个操作中都指定了一个上下文)。删除AppDelegate中的所有CoreData生成的代码解决了这个问题。