【问题标题】:How to use XCTest framework to test REST based native iOS appliction?如何使用 XCTest 框架测试基于 REST 的原生 iOS 应用程序?
【发布时间】:2015-06-27 16:32:43
【问题描述】:
尝试检查如何将 XCTest 框架用于使用 REST Web 服务的应用程序。但是没有找到任何好的和完整的示例教程。
我只有 XCTest 的一些基本概念,但对将 XCTest 与 REST 应用程序一起使用感到困惑。
谁能给我一些如何使用它的例子。我只需要想法和方向,然后我就可以走了。
注意:如果应用程序是基本应用程序,我知道,但我的困惑只是针对基于 REST 的 iOS 应用程序测试。
【问题讨论】:
标签:
ios
unit-testing
xctest
【解决方案1】:
我正在使用XCTestExpectation 测试基于 REST 协议的客户端-服务器通信。
这是高级示例:
- (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);
}];
}
这个测试涵盖了这样的期望案例:
1. 服务器必须在 30 秒或更短的时间内返回响应;
2. 服务器必须返回有效的 JSON 对象和有效的响应码;
3. JSON 对象必须具有正确的结构才能映射到用户对象。