小编jlu*_*jan的帖子

AFNetworking 2.0和单元测试

我正在尝试使用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)

xcode ios xctest afnetworking-2

5
推荐指数
1
解决办法
1872
查看次数

标签 统计

afnetworking-2 ×1

ios ×1

xcode ×1

xctest ×1