我正在测试一些使用Grand Central Dispatch进行异步处理的代码.测试代码如下所示:
[object runSomeLongOperationAndDo:^{
STAssert…
}];
Run Code Online (Sandbox Code Playgroud)
测试必须等待操作完成.我目前的解决方案如下:
__block BOOL finished = NO;
[object runSomeLongOperationAndDo:^{
STAssert…
finished = YES;
}];
while (!finished);
Run Code Online (Sandbox Code Playgroud)
看起来有点粗糙,你知道更好的方法吗?我可以通过调用暴露队列然后阻塞dispatch_sync:
[object runSomeLongOperationAndDo:^{
STAssert…
}];
dispatch_sync(object.queue, ^{});
Run Code Online (Sandbox Code Playgroud)
......但是这可能会暴露太多object.
我正在尝试使用XCTest对在XCode 5中使用AFNEtworking的类进行单元测试.我遇到的问题是我的AFHTTPRequestOperation的完成块永远不会被执行.我假设这是运行单元测试的XCode和AFNetworking的调度队列之间的一些脱节.以下测试用例通过,但永远不会到达完成块中的NSLog语句(没有日志输出,也没有捕获这些语句上设置的断点).相同的代码在单元测试之外工作.有谁知道如何解决这个问题?我正在使用Nocilla来模拟实际的请求,结果是使用真正的服务器重新调整有效响应?
编辑以使测试失败并在块中设置日志变量
- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
[[LSNocilla sharedInstance] start];
stubRequest(@"POST", @"http://www.example.com/module/api/ping").
andReturn(200).
withHeaders(@{@"Content-Type": @"application/json"}).
withBody(@"{\"success\":true}");
stubRequest(@"GET", @"http://www.example.com/module/api/ping?testkey=testval").
andReturn(200).
withHeaders(@{@"Content-Type": @"application/json"}).
withBody(@"{\"success\":true}");
}
- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
[[LSNocilla sharedInstance] stop];
[[LSNocilla sharedInstance] clearStubs];
}
- (void)testSanity
{
AFSecurityPolicy *policy = …Run Code Online (Sandbox Code Playgroud)