RestKit RKMappingTest JSON array

淺唱寂寞╮ 提交于 2019-12-02 04:37:37

You're thinking about testing, and more specifically the scope of testing, wrongly. This is a unit test, and the unit is the mapping. The mapping deals with individual items and that is why your modified test works - because the scope is correct.

Response descriptors have a different scope. You can't test the scope of a response descriptor in a mapping test.

I think I've found a somewhat better answer than the reply "you're testing wrong". You should be able to test properly by wrapping your mapping under test in another mapping.

// Data (in your case, you've used a hard coded string here)
id fixtureData = [RKTestFixture parsedObjectWithContentsOfFixture:@"fixture.json"];

// Setup
RKObjectMapping* wrapperMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
RKObjectMapping *mappingToTest = [RKObjectMapping mappingForClass:[PlayerVO class]];
[mappingToTest addAttributeMappingsFromArray:@[@"firstName", @"middeName", @"lastName", @"dob", @"sex"]];
[wrapperMapping addRelationshipMappingWithSourceKeyPath:@"players" mapping:mappingToTest];
RKMappingTest* test = [RKMappingTest testForMapping:wrapperMapping sourceObject:fixtureData destinationObject:nil];

// If you're dealing with a managed object mapping, you'll need these:
// I usually set up a single store in the "+(void)setUp" method and reset it
// on each test in the "-(void)setUp" method. (note one method is class, one
// is instance).
//test.managedObjectContext = managedObjectStore.persistentStoreManagedObjectContext;
//test.mappingOperationDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext cache:[RKFetchRequestManagedObjectCache new]];

// Expectations
[test addExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"..." destinationKeyPath:@"..."]];
// Add others as you see fit (these will work for the mappingToTest mapping).

// Evaluate
XCTAssert([test evaluate]);
NSDictionary* result = test.destinationObject;
// You can verify counts on the result and any other objects.
// If you have variables in other structures you want tracked, you can expand
// on the wrapper mapping. i.e. If your data contained a "teamName" at the
// same level as your array of "players", you could add a mapping from
// "teamName" to an arbitrary keyPath for your dictionary then access it in the
// result variable.

This has worked for me. I think it's short sighted to say that you shouldn't test collection mappings. You may have a poorly designed REST API that necessitates this, or a complex interaction between managed objects that you want to verify is set up correctly when mapping takes place.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!