I have created a mock UINavigationController using OCMock. However, I cannot assign it to the navigationController property of a UIViewController since that property is readonly.
id mockNavController = [OCMockObject mockForClass:[UINavigationController class]];
...
myViewController.navigationController = mockNavController; // readonly!
The author of this blog post claims to have found a solution but neglected to share it.
It's not necessary to create a mutator that allows you to set the navigationController property, as you can mock the accessor that returns it. Here's how I do it:
-(void)testTappingSettingsButtonShouldDisplaySettings {
MyController *myController = [[MyController alloc] init];
// expect the nav controller to push a settings controller
id mockNavigationController = [OCMockObject mockForClass:[UINavigationController class]];
[[mockNavigationController expect] pushViewController:[OCMArg any] animated:YES];
// set up myController to return the mocked navigation controller
id mockController = [OCMockObject partialMockForObject:myController];
[[[mockController expect] andReturn:mockNavigationController] navigationController];
[myController settingsButtonTapped];
[mockNavigationController verify];
[mockController verify];
[myController release];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With