ios - 为什么奇异鸟匹配器在匹配时会引发异常?

标签 ios objective-c kiwi

我已经实现了来自 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 

最佳答案

测试没有找到文件 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];
}];

关于ios - 为什么奇异鸟匹配器在匹配时会引发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16832133/

相关文章:

ios - 使用 CoreLocation 重要位置监控和 CoreBluetooth 时的电池消耗

ios - faSTLane 构建问题 - 双倍路径名

ios - 如果我们覆盖保留/释放方法,我们是否会失去对 ARC 的 objc_retain()/objc_release() 优化?

ios - 无法在 Xcode 6 Beta 5 上运行测试

ios - "Scollable content size ambiguity"尽管硬编码宽度和高度?

ios - 无法控制 Storyboard中 YTPlayerView 实例的显示大小

objective-c - CGRectGetWidth 与 CGRect.size.width

ios - 来自 plist 文件的 NSDictionary

ios - Kiwi 单元测试在 Xcode 中从未失败

ios - MagicalRecord MR_createInContext : returns nil