我已经从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
答案 0 :(得分:1)
测试找不到文件test.json,所以它返回,这就是你得到零的原因。
在与测试文件相同的文件夹中创建一个文件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];
}];