hoc*_*ker 11 unit-testing objective-c ocunit
我一直在寻找一种方法来使用SenTestingKit在我的客户端代码和服务器之间进行一些集成测试.我没有运气.似乎一旦代码在方法中运行,对象就会被破坏.这意味着任何异步响应都不会调用选择器.
问题:
仅供参考,我正在运行测试服务器,我知道预期的结果.
我已经做了很多谷歌搜索,但没有看到这种或那样的证据.我相信其他人也会感兴趣.
sam*_*ize 30
您可以使用信号量等待异步方法完成.
- (void)testBlockMethod {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
// Your block method eg. AFNetworking
NSURL *url = [NSURL URLWithString:@"http://httpbin.org/ip"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]);
STAssertNotNil(JSON, @"JSON not loaded");
// Signal that block has completed
dispatch_semaphore_signal(semaphore);
} failure:nil];
[operation start];
// Run loop
while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
dispatch_release(semaphore);
Run Code Online (Sandbox Code Playgroud)
}
http://samwize.com/2012/10/03/sentestingkit-does-not-support-wait-for-blocks/