【发布时间】:2014-08-05 18:04:50
【问题描述】:
我在测试类中有 2 种性能测试方法。如果我单独运行它们,它们会通过。如果我运行孔类方法,它们会失败并显示消息:
**** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“API 违规 - 多次调用 -[XCTestExpectation 完成]。”*
有没有办法将一对性能测试包含在一个类中?
这里是示例代码:
- (void)testPerformanceAuthenticateWithLogin {
XCTestExpectation *authenticateExpectation = [self expectationWithDescription:@"Authenticate With Login"];
__block int userID = 0;
[self measureBlock:^{
[AuthenticationService authenticateWithLogin:email password:password success:^(AuthenticationResponse *result) {
XCTAssert(result.isAuthenticated);
userID = result.userID;
[authenticateExpectation fulfill];
} failure:^(NSError *error) {
XCTAssertNil(error);
}];
}];
[self waitForExpectationsWithTimeout:3 handler:^(NSError *error) {
XCTAssertNil(error);
[AuthenticationService logoutWithServicePersonID:userID success:nil failure:nil];
}];
}
- (void)testPerformanceGetServicePersonByID {
XCTestExpectation *getServicePersonExpectation = [self expectationWithDescription:@"get Service Person By ID"];
__block int userID = 0;
[AuthenticationService authenticateWithLogin:email password:password success:^(AuthenticationResponse *result) {
userID = result.userID;
[self loginSuccess:result];
[self measureBlock:^{
[ServicePersonService getServicePersonByIDWithServicePersonID:userID success:^(ServicePersonDTO *result) {
XCTAssertNotNil(result);
[getServicePersonExpectation fulfill];
} failure:^(NSError *error) {
XCTAssertNil(error);
}];
}];
} failure:^(NSError *error) {
XCTAssertNil(error);
}];
[self waitForExpectationsWithTimeout:3 handler:^(NSError *error) {
XCTAssertNil(error);
[AuthenticationService logoutWithServicePersonID:userID success:nil failure:nil];
}];
}
【问题讨论】:
-
我在同一个类中有多个测试,每个测试都有一个 XCTestExpectation 并且工作正常。虽然我没有使用 measureBlock 。如果每次通过时只运行一个 measureBlock 是否有效?
标签: objective-c xctest