Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking class methods with OCMock not working

What's wrong with this code? Trying to make a class method return @NO, but it's returning @YES (the NSLog prints "1").

// Make the test think you don't have any internet
id mockFunctions = [OCMockObject mockForClass:[Functions class]];
[[[mockFunctions stub] andReturnValue:@NO] connectionAvailable];


NSLog(@"derp: %hhd", [Functions connectionAvailable]);
like image 413
Luke The Obscure Avatar asked Feb 02 '26 01:02

Luke The Obscure


2 Answers

I had two copies of the class that was being mocked. By not including the .m file in the test target, everything worked fine.

like image 132
Luke The Obscure Avatar answered Feb 03 '26 18:02

Luke The Obscure


The construction for andReturnValue is a little more complex than what you are doing.

[[[mockFunctions stub] andReturnValue:OCMOCK_VALUE((BOOL){NO})] connectionAvailable];

Give that a try and see if it works.

like image 43
Ben Flynn Avatar answered Feb 03 '26 18:02

Ben Flynn