2

This is how my test looks like:

// 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 is a subclass of AFHTTPSessionManager (from AFNetworking).

Each time this test is executed there is an EXC_BAD_ACCESS error like this:

EXC_BAD_ACCESS (code=1, address=0x7a0090020)

Frankly, I'm not particularly experienced with debugging EXC_BAD_ACCESS errors, and the error message doesn't seem to be very useful

Also, the strange this is that this only happens when I use the POST:parameters:success:failure:, but not with its GET counterpart (which has exactly the same parameters).

Could this me a problem inside AFNetworking?

EDIT:

Enabled Zombie Objects for Test, this is the result:

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

EDIT 2:

I reduced my test case to the following code which always reproduces the crash:

#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
iosdude
  • 1,131
  • 10
  • 27

1 Answers1

2

This appears to be a known issue:

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

I did the following workaround as suggested in the issue discussion:

  1. Change OCMVerify to OCMExpect
  2. Move your OCMExpect code before your test makes a call to the expected method
  3. Call OCMVerifyAll in the end
iosdude
  • 1,131
  • 10
  • 27