Nilesh Agrawal
Nilesh Agrawal

Reputation: 3042

How to use OCMock to verify static methods

I am trying to use OCMock library. I am trying to create mock of class object, but it is failing to verify the method. I am unable to understand why the tests are failing.

@interface MyClass:NSObject
+(void) someMethod;
@end
@implementation MyClass
+(void) someMethod
{

    NSError* error = nil;
    if (![Utility isValidPropWithError:&error])
    {
        [Logger log:LoggerLevelWarning message:[error localizedDescription] className:className];
    }
}
@end

Test :

-(void)testIfLoggerIsset{
    id partialMockLogger = OCMClassMock([Logger class]);
    id partialMockUtility = OCMClassMock([Utility class]);
    id partialMockClass = OCMClassMock([MyClass class]);
    NSError *error = nil;
    OCMExpect([partialMockUtility isValidPropWithError:&error]);

    [MyClass someMethod];

    //This works fine.
    OCMVerifyAll(partialMockClass);
    NSString *className = @"classname";
    //This is failing...
    OCMVerify([partialMockUtility isValidPropWithError:&error]);
    OCMVerifyAll(partialMockUtility);
    //This is failing...
    OCMVerify([partialMockLogger log:LoggerLevelWarning message:[error localizedDescription] className:className]);
    [partialMockUtility stopMocking];
    [partialMockLogger stopMocking];
}

In the above code, although [Utility isValidPropWithError:&error]; is called OCMVerify([partialMockUtility isValidPropWithError:&error]);is failing.

Upvotes: 0

Views: 1179

Answers (1)

Jordan
Jordan

Reputation: 4212

Several things here:

First, OCMVerify([partialMockUtility isValidPropWithError:&error] is failing because you are expecting the address of the NSError object you created in the test to be passed to isValidPropWithError:, but in MyClass +someMethod you are creating a different NSError object. The addresses of two different objects will not be the same.

To fix this, change your expectation and verification to:

OCMExpect([partialMockUtility isValidPropWithError:(NSError __autoreleasing**)[OCMArg anyPointer]]);
OCMVerify([partialMockUtility isValidPropWithError:(NSError __autoreleasing**)[OCMArg 

and just ignore the actual value of the parameter and expect that it's going to be an NSError pointer (since you're creating it inside of someMethod, there's no way to know what it's going to be before you call the method).

Second, since you are already explicitly verifying +isValidPropWithError, OCMVerifyAll(partialMockUtility) isn't going to verify anything. You should either explicitly verify all of your expectations, or simply use OCMVerifyAll(partialMockUtility) and let it verify all your expectations and don't bother with expecting the specific call. OCMVerifyAll will verify everything you expect on the mock object you give it. This isn't going to cause a test failure - both calls will pass, since you've already verified the call the first time, the call to OCMVerifyAll() isn't going to have anything to verify, so it will pass.

Last, OCMVerify([partialMockLogger log:LoggerLevelWarning message:[error localizedDescription] className:className]); is failing because you didn't set an expectation for it.

Upvotes: 1

Related Questions