[英]How to test a sendAsynchronousRequest: on XCTest
Sorry for the noob question, but I'm trying to test a connection, but I'm not able do verify the return of the request, because the test ends before the completion handler have a chance to execute. 对于noob问题很抱歉,但我正在尝试测试连接,但是我无法验证请求的返回,因为测试在完成处理程序有机会执行之前结束。 To ilustrate:
说明:
-(void)testConnection
{
[[Conector sharedInstance] performAsynchronousRequestWithServerRequest:_srvRequest completionHandler:^(RequestAsynchronousStatus finishStatus, NSData *data) {
if (finishStatus == RequestAsynchronousOK){
_data = data;
NSLog(@"Data OK");
}
}];
XCTAssertNotNil(_data, @"Data was nil");
}
When I try to assert, _data will always be nil, because the completion handler wasn't executed yet. 当我尝试断言时,_data将始终为nil,因为还没有执行完成处理程序。 There is a mechanism to force the test to wait until I have some response from the sendAsynchronousRequest: method.
有一种机制可以强制测试等待,直到我收到sendAsynchronousRequest:方法的响应。 Thanks in advance.
提前致谢。
This looks like exactly what you need: 这看起来正是您所需要的:
XCAsyncTestCase : Asynchronous capable SenTestCase subclass. XCAsyncTestCase :具有异步功能的SenTestCase子类。
Basically, you should write your test like this: 基本上,你应该写这样的测试:
- (void)testConnection
{
[[Conector sharedInstance] performAsynchronousRequestWithServerRequest:_srvRequest completionHandler:^(RequestAsynchronousStatus finishStatus, NSData *data) {
if (finishStatus == RequestAsynchronousOK)
{
_data = data;
[self notify:XCTAsyncTestCaseStatusSucceeded];
NSLog(@"Data OK");
}
}];
[self waitForTimeout:10];
XCTAssertNotNil(_data, @"Data was nil");
}
Notice the waitForTimeout:
call and the notify:
calls. 注意
waitForTimeout:
call和notify:
calls。 10 seconds should be enough for the test, though it would depend on the request itself. 10秒应该足以进行测试,但这取决于请求本身。
You could even get more specific and wait for a certain status, like so: 您甚至可以更具体地等待某种状态,如下所示:
[self waitForStatus: XCTAsyncTestCaseStatusSucceeded timeout:10];
This way, if the connection fails to notify the XCTAsyncTestCaseStatusSucceeded
status, the wait call will timeout and your test would fail (as it should). 这样,如果连接无法通知
XCTAsyncTestCaseStatusSucceeded
状态,则等待调用将超时,您的测试将失败(应该如此)。
Here's another alternative, XCAsyncTestCase based upon the GHUnit version: 这是另一个替代方案,基于GHUnit版本的XCAsyncTestCase:
https://github.com/iheartradio/xctest-additions https://github.com/iheartradio/xctest-additions
Usage is the same, just import and subclass XCAsyncTestCase. 用法是相同的,只是导入和子类XCAsyncTestCase。
@implementation TestAsync
- (void)testBlockSample
{
[self prepare];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(){
sleep(1.0);
[self notify:kXCTUnitWaitStatusSuccess];
});
// Will wait for 2 seconds before expecting the test to have status success
// Potential statuses are:
// kXCTUnitWaitStatusUnknown, initial status
// kXCTUnitWaitStatusSuccess, indicates a successful callback
// kXCTUnitWaitStatusFailure, indicates a failed callback, e.g login operation failed
// kXCTUnitWaitStatusCancelled, indicates the operation was cancelled
[self waitForStatus:kXCTUnitWaitStatusSuccess timeout:2.0];
}
This is now included in XCode 6: 现在包含在XCode 6中:
Obj-C example here: 这里的Obj-C示例:
XCTest and asynchronous testing in Xcode 6 Xcode中的XCTest和异步测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.