1

我已经实现了Mocking Network Requests With OHHTTPStubs的示例。不幸的是,我在匹配我的结果时遇到了 EXC_BAD_ACCESS 异常:

 [[expectFutureValue(origin) shouldEventuallyBeforeTimingOutAfter(3.0)] equal:@"111.222.333.444"];

有没有人遇到过这种问题?有什么可能的解决方案?

这是完整的代码:

#import "Kiwi.h"
#import "AFNetworking.h"
#import "OHHTTPStubs.h"
#import "OHHTTPStubsResponse.h"

SPEC_BEGIN(NetworkTest)

describe(@"The call to the external service", ^{

    beforeEach(^{
        [OHHTTPStubs addRequestHandler:^OHHTTPStubsResponse*(NSURLRequest *request, BOOL onlyCheck){
            return [OHHTTPStubsResponse responseWithFile:@"test.json" contentType:@"text/json" responseTime:1.0];
         }];
     );

    it(@"should return an IP address", ^{

        __block NSString *origin;
        NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://httpbin.org/ip"]];

        AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
            origin = [JSON valueForKeyPath:@"origin"];
        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)     {
            // no action
        }];

        [operation start];

        [[expectFutureValue(origin) shouldEventuallyBeforeTimingOutAfter(3.0)] equal:@"111.222.333.444"];

    });

});

SPEC_END 
4

1 回答 1

1

测试没有找到文件 test.json 所以它返回,这就是你得到 nil 的原因。

在与您的测试文件相同的文件夹中创建一个文件 test.json 并放置您需要查看测试通过或失败的正文。

看到测试失败

{ "origin" : "1.2.3.4"}

查看测试通过

{ "origin" : "111.222.333.444"}

//注意添加请求处理程序已被弃用,以下将起作用

[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
    return YES; // Stub ALL requests without any condition
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
    // Stub all those requests with our "response.json" stub file
    return [OHHTTPStubsResponse responseWithFile:@"test.json" contentType:@"text/json" responseTime:1.0];
}];
于 2013-08-12T14:13:33.627 回答