Reputation: 157
I am not able to get my mocks made with OCMock to propagate into my implementation code during test.
I have installed OCMock into my iOS project with CocoaPods, and have tried tried multiple class mock and partial object mock implementations.
For instance:
+ (int) returnsFive {
return 5;
}
#import "MyClass.h"
- (void) viewDiDLoad {
self.number = [MyClass returnsFive];
}
id myClassMock = [[MyClass alloc] init];
OCMStub([myClassMock returnsFive]).andReturn(7);
NSLog(@"%@", [MyClass returnsFive]); // -- ((this prints out 7))
[MyController layoutIfNeeded]; // -- ((runs viewDidLoad))
XCTAssertEqual(MyController.number, 7, @"the number property on MyController should be set to 7 after mock");
// -- ((this fails, MyController.number equals 5 and not 7 as stubbed out))
The class mock works when run in the test, however, when run in the implementation code, the mock does not carry over. The actual class (i.e. MyClass) gets run instead of the mock.
We have tried this with different variations of mocking (i.e. partial mocks on objects and using mocks on instance methods instead of class methods). All have yielded the exact same result where the mock does not propagate into the implementation code.
1.) Is this even possible? (it seems like it must be)
2.) Is there some setup step we are missing? (we installed OCMock with CocoaPods, and imported the framework into our testClass with #import )
3.) Any ideas on how to solve our dilemma???
Upvotes: 0
Views: 110
Reputation: 1177
You can see a fully functional example here:
https://github.com/uShip/ocmock-singletons-example
Look at test named "testToggleOnRegistersForPushNotifications_On_OCMock_NoDI". It creates a mock for UIApplication and verifies that some methods of the mock instance get called.
Upvotes: 0