OCMArg checkWithBlock与EXC_BAD_ACCESS

时间:2015-11-18 07:14:06

标签: ios afnetworking exc-bad-access ocmock

这就是我的测试结果:

// In setUp:
self.APIClientMock = OCMClassMock([APIClient class]);
OCMStub([self.APIClientMock sharedClient]).andReturn(self.APIClientMock);

// In test method:
OCMVerify([self.APIClientMock POST:@"invitations" parameters:[OCMArg checkWithBlock:^BOOL(NSDictionary *parameters) {
    // Some parameters check is supposed to be here, but even simply returning YES doesn't work...
    return YES;
}] success:[OCMArg any] failure:[OCMArg any]]);

APIClient是AFHTTPSessionManager的子类(来自AFNetworking)。

每次执行此测试时都会出现EXC_BAD_ACCESS错误,如下所示:

EXC_BAD_ACCESS (code=1, address=0x7a0090020)

坦率地说,我在调试EXC_BAD_ACCESS错误方面没有特别的经验,而且错误信息似乎并不是非常有用

另外,奇怪的是,这只发生在我使用POST:parameters:success:failure:时,而不是GET对应物(具有完全相同的参数)。

这可能是我在AFNetworking中遇到的问题吗?

修改

启用Zombie Objects for Test,结果如下:

*** -[__NSDictionaryI retain]: message sent to deallocated instance 0x7fbd8306f280

编辑2:

我将测试用例缩减为以下代码,该代码总是重现崩溃:

#import <AFNetworking.h>
#import <OCMock/OCMock.h>
#import <XCTest/XCTest.h>

@interface ExampleTests : XCTestCase

@end

@implementation ExampleTests

- (void)sendRequesUsingSessionManager:(AFHTTPSessionManager *)sessionManager {
    NSDictionary *parameters = @{@"param": @"value"};
    [sessionManager POST:@"test" parameters:parameters success:nil failure:nil];
}

- (void)testExample {
    id sessionManagerMock = OCMClassMock([AFHTTPSessionManager class]);
    [self sendRequesUsingSessionManager:sessionManagerMock];
    OCMVerify([sessionManagerMock POST:@"test" parameters:[OCMArg checkWithBlock:^BOOL(NSDictionary *parameters) {
        return YES;
    }] success:[OCMArg any] failure:[OCMArg any]]);
}

@end

1 个答案:

答案 0 :(得分:2)

这似乎是一个已知问题:

https://github.com/erikdoe/ocmock/issues/147

我按照问题讨论中的建议做了以下解决方法:

  1. cond <- sapply(seq(nrow(a)), # check each row, one by one function (i){ !(a$a1[i] %in% b$b1) | # a1 of the specific row is not in b1 or !(a$a2[i] %in% b$b2[b$b1==a$a1[i]]) # a2 of the specific row is not in the values of b2 for which b1 equals a1 of the sepcific row }) a[cond, ] # a1 a2 #2 1 1 #5 4 2 更改为s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n'] s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n'] for line in context_diff(s1, s2, fromfile='before.py', tofile='after.py'): sys.stdout.write(line)
  2. 在测试调用预期方法之前移动*** before.py --- after.py *************** *** 1,4 **** ! bacon ! eggs ! ham guido --- 1,4 ---- ! python ! eggy ! hamster guido 代码
  3. 最后致电OCMVerify