Few days ago I understood how should I write test for RestKit with OCMock.
method to test:
- (void)signUpWithUsername:(NSString *)username email:(NSString *)email password:(NSString *)password block:(SignUpBlock)block {
if (!username || !email || !password) {
return;
}
CTFUser *user = [CTFUser createObject];
user.username = username;
user.email = email;
user.password = password;
[_connection.manager postObject:user path:@"path" parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
block(YES);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
block(NO);
}];
}
test case (check if custom block has been called when success block has been called by rest kit): - (void)testBlockShouldReturnYesIfSuccess { id mockClient = [OCMockObject mockForClass:[AFHTTPClient class]];
RKObjectManager *manager = [[RKObjectManager alloc] init];
manager.HTTPClient = mockClient;
id mockMananger = [OCMockObject partialMockForObject:manager];
[[[mockMananger expect] andDo:^(NSInvocation *invocation) {
void (^successblock)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) = nil;
[invocation getArgument:&successblock atIndex:5];
successblock(nil, OCMOCK_ANY);
}] postObject:OCMOCK_ANY path:OCMOCK_ANY parameters:nil success:OCMOCK_ANY failure:OCMOCK_ANY];
SignUpBlock block = ^(BOOL success) {
XCTAssertTrue(success, @"");
};
CTFAPIConnection *connection = [[CTFAPIConnection alloc] initWithManager:mockMananger];
_accounts = [[CTFAPIAccounts alloc] initWithConnection:connection];
id mockAccounts = [OCMockObject partialMockForObject:_accounts];
[mockAccounts signUpWithUsername:@"username" email:@"email" password:@"password" block:block];
}
This test pass. But how to test if mapping for this request and for response is OK? This test doesn't check that Should I make some JSON
fixture with response and request? What's next?
What I want to test? - If request and response mapping is OK (this is hard for me to realized how test should looks like)
Thank you in advance
Ok I found some nice resource. https://github.com/RestKit/RestKit/wiki/Unit-Testing-with-RestKit. If I understood I should test mapping with
JSON
fixtures isolated from real requests and second good way is to create some test server which only return some fixtures.