我已经实现了来自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 发布于 2013-08-12 22:13:34
测试没有找到文件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];
}];https://stackoverflow.com/questions/16832133
复制相似问题