How to use XCTest framework to test REST based native iOS appliction?

1.5k views Asked by At

Tried to check out how to use XCTest framework for an application which is using REST web services. But did not find any good and complete example tutorial.

I have only some basic idea of XCTest but confused to use XCTest with REST application.

Can anybody give me some examples of how to use this. I just need the idea and direction and then I can go.

Note: If the application is a basic application, I know but my confusion is only for REST based iOS application testing.

1

There are 1 answers

0
Vlad Papko On

I am using XCTestExpectation for testing client-server communication that is based on REST protocol.
Here is high level example:

- (void)testCreatingUserRequest {
    // Initialize necessary objects
    RestManager *restManager = ...
    CreatingUserRequest *request = ...

    // Execute test
    XCTestExpectation *expectation = [self expectationWithDescription:@"Create User"];

    [restManager createUserWithRequest:request completionHandler:^(NSDictionary *JSONObject, NSError *error) {
        XCTAssertNotNil(JSONObject);
        XCTAssertNotNil([User userWithJSONObject:JSONObject]);
        XCTAssertNil(error);

        [expectation fulfill];
    }];

    [self waitForExpectationsWithTimeout:30 handler:^(NSError *error) {
        XCTAssertNil(error);
    }];
}

This test cover such expectation cases:
1. Server has to return response in 30 seconds or less;
2. Server has to return valid JSON object with valid response code;
3. JSON object has to have correct structure for mapping to User object.