Cannot stub a method ([NSData initWithBase64EncodedString])

330 views Asked by At

I'm newcomer to Objective-C and OCMock. I created this trivial test and expect it to pass, but it fails:

-(void)testMock
{
    id dataMock = [OCMockObject niceMockForClass:[NSData class]];
    [[[dataMock stub] andReturn:dataMock] initWithBase64EncodedString:[OCMArg any]
         options:[OCMArg any]];

    NSData *ret = [dataMock initWithBase64EncodedString:@"dGVzdA==" 
        options:NSDataBase64DecodingIgnoreUnknownCharacters];
    XCTAssertEqualObjects(dataMock, ret);
}

As you see, I stub a method and command it to return dataMock, but it returns null:

Assertions:  ((dataMock) equal to (ret)) failed: 
    ("OCMockObject(NSData)") is not equal to ("(null)")

Note: Testing with init instead of initWithBase64EncodedString, works as expected and passes.

I'm using XCode 6.0.1 and programming for iOS 8.0.

1

There are 1 answers

0
Mousa On BEST ANSWER

I found out that this the famous problem of matching primitives in OCMock. options in the mentioned method, takes a primitive and [OCMArg any] does not match it.

So for now I'm going with the exact used value:

[[[dataMock stub] andReturn:dataMock] initWithBase64EncodedString:[OCMArg any]
     options:NSDataBase64DecodingIgnoreUnknownCharacters];