Search code examples
iosobjective-cxcodeunit-testingocmock

Is there a way to change one of parameters in OCMock and continue running the stubbed function


Hello I'm trying to change the parameter of a function I have stubbed. I see in logs that my stub is called successfully, and I want to change one of the paramters and run the original function. Can I do that with OCMock?

Here's what I'm doing:

.... 

id testMgrMock = [OCMockObject partialMockForObject:testClassInstance];

OCMStub([testMgrMock addRequestWithMethod:OCMOCK_ANY
                                     path:OCMOCK_ANY
                               parameters:OCMOCK_ANY
                                  headers:OCMOCK_ANY
                                     body:OCMOCK_ANY
                                 delegate:OCMOCK_ANY
                             successBlock:OCMOCK_ANY
                             failureBlock:OCMOCK_ANY]).
andDo(^(NSInvocation* invocation){
    NSString* path = @"http://addressnotexi.st";
    [invocation setArgument:&path atIndex:2];

    // here I want to call "addRequestWithMethod:path:parameters:header (etc)" 
    // on a real object with the parameter "path" changed to my string

    [invocation invoke];
});
......

the line [invocation invoke]; gives me bad_exec crash. Even if I don't change the parameter.

How to do it properly? Is it possible at all?

Thank you


Solution

  • I found the problem

    Need to reaarange code in the following way:

    before @implementation:

    NSString invalidPath = @"http://theaddressthatdoesnotexis.ts";
    

    And the code will look like this:

    .... 
    
    id testMgrMock = [OCMockObject partialMockForObject:testClassInstance];
    
    OCMStub([testMgrMock addRequestWithMethod:OCMOCK_ANY
                                         path:[OCMArg isNotEqual:invalidPath]
                                   parameters:OCMOCK_ANY
                                      headers:OCMOCK_ANY
                                         body:OCMOCK_ANY
                                     delegate:OCMOCK_ANY
                                 successBlock:OCMOCK_ANY
                                 failureBlock:OCMOCK_ANY]).
    andDo(^(NSInvocation* invocation){
        [invocation setArgument:&invalidPath atIndex:3];
        [invocation invoke];
    });
    ......
    

    In shorter words. My invoke was working but if was calling itself