Reputation: 151
Passing an OCMock object to a method where the function calls isKindOfClass. However for unit test, the value returned is not of the mocked class but OCMockObject
Upvotes: 12
Views: 821
Reputation: 2589
If you want to pass OCMock object to a method where the function calls isKindOfClass you need to create partial mock. Following code might help you. It worked for me.
-(void)testMyTest
{
FirstViewController* masterVC = [[FirstViewController alloc]init];
SecondViewController *second = [[SecondViewController alloc] init];
id master = [OCMockObject partialMockForObject:second];
[[master expect] getName:@"PARAM"];
[masterVC doSomething:master];
[master verify];
[masterVC release];
[second release];
}
doSomething method inside FirstViewController
-(void)doSomething:(SecondViewController *)detail
{
if ([detail isKindOfClass:[SecondViewController class]])
{
NSString * returnVal = [detail getName:@"PARAM"];
NSLog(@"returnVal %@",returnVal);
}
}
One more alternative is mocking the isKindOfClass method, So the test case will become
- (void)testMyTest
{
CalculatorViewController* masterVC = [[CalculatorViewController alloc]init];
id master = [OCMockObject niceMockForClass:[SecondViewController class]];
BOOL ret = YES;
[[[master expect] andReturnValue:OCMOCK_VALUE(ret)] isKindOfClass:[SecondViewController class]];
[[master expect] getName:@"PARAM"];
[masterVC doSomething:master];
[master verify];
}
Upvotes: 0
Reputation: 4059
Here is a whole article explaining how exactly to write the OCMock isKindOfClass
method (it is not existing by default) and how to use it: http://blog.carbonfive.com/2009/02/17/custom-constraints-for-ocmock/
Upvotes: 1