加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – AFNetworking 2.0和单元测试

发布时间:2020-12-14 19:15:33 所属栏目:百科 来源:网络整理
导读:我正在尝试使用XCTest对在XCode 5中使用AFNEtworking的类进行单元测试.我遇到的问题是我的AFHTTPRequestOperation的完成块永远不会被执行.我假设这是运行单元测试的XCode和AFNetworking的调度队列之间的一些脱节.以下测试用例通过,但永远不会到达完成块中的N
我正在尝试使用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 = [[AFSecurityPolicy alloc] init];
    //[policy setAllowInvalidCertificates:YES];

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.example.com/module/api/ping"]];
    //manager.operationQueue = [NSOperationQueue mainQueue];
    [manager setSecurityPolicy:policy];


    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];


    __block id resObj = nil;
    __block id resError = nil;

    AFHTTPRequestOperation *req = [manager POST:@"http://www.example.com/module/api/ping"
                                    parameters:[NSDictionary dictionaryWithObject:@"testval" forKey:@"testkey"]
                                       success:^(AFHTTPRequestOperation *operation,id responSEObject) {

                                           NSLog(@"Response: %@",responSEObject);
                                           resObj = responSEObject;
                                           return;

                                       }
                                       failure:^(AFHTTPRequestOperation *operation,NSError *error) {

                                            NSLog(@"Error: %@",error);
                                           resError = error;
                                           return;
                                       }];
    [req waitUntilFinished];

    NSLog(@"req.status: %d",req.response.statusCode);
    NSLog(@"req.responSEObj: %@",req.responSEObject);
    XCTAssertTrue(req.isFinished);
    NSLog(@"resObj: %@",resObj);
    NSLog(@"resError: %@",resError);
    XCTAssertEqual([[req.responSEObject objectForKey:@"success"] boolValue],YES);

    XCTAssertEqual([[resObj objectForKey:@"success"] boolValue],YES);
}

控制台输出

Test Case '-[AppSupportTests testSanity]' started.
2014-04-29 16:45:07.424 xctest[72183:303] req.status: 200
2014-04-29 16:45:07.424 xctest[72183:303] req.responSEObj: {
    success = 1;
}
2014-04-29 16:45:07.424 xctest[72183:303] resObj: (null)
2014-04-29 16:45:07.425 xctest[72183:303] resError: (null)
/Users/jlujan/Code/AppSupport/AppSupportTests/AppSupportTests.m:114: error: -[AppSupportTests testSanity] : (([[resObj objectForKey:@"success"] boolValue]) equal to (__objc_yes)) failed: ("NO") is not equal to ("YES")
Test Case '-[AppSupportTests testSanity]' failed (0.003 seconds).

解决方法

根据评论中的讨论我们发现 waitUntilFinished is once the background operation is complete,并且它不会等到完成块被调用之后.

有一个更好的异步测试框架 – Expecta.
然后而不是调用:

XCTAssertTrue(req.isFinished);
XCTAssertEqual([[resObj objectForKey:@"success"] boolValue],YES);

你可以做:

expect(req.isFinished).will.beTruthy();
expect([[resObj objectForKey:@"success"] boolValue]).will.beTruthy();

有lots of other matchers,只需确保在setUp方法中将timeout设置为+[Expecta setAsynchronousTestTimeout:].

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读