简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM