I was just practising OCMock
, The problem I am facing here is
I have one method named foo
which returns CGRect
, this method is called from another method callFoo
.
-(CGRect)foo {
return CGRectMake(10, 10, 10, 10);
}
-(void)callFoo {
CGRect rect = [self foo];
NSLog(@"%@",NSStringFromCGRect(rect));
}
My unit test for callFoo
method is bellow.
- (void)test__callFoo__invokesWithMockingFoo
{
ViewController *sut = [[ViewController alloc] init];
id master = [OCMockObject partialMockForObject:sut];
CGRect rect = CGRectMake(0, 0, 0, 0);
[[[master expect] andReturnValue:OCMOCK_VALUE(rect)] foo];
[sut callFoo];
[master verify];
}
When this test case runs,I am getting an unexpected crash when it reaches CGRect rect = [self foo];
I wanted to know why this crash is happening if we return CGRect
from mocked method
and how to resolve this crash.
Could any one please help to resolve this problem.
Thanks in advance.
P.S : This works fine if I replace CGRect with NSValue. As shown below
-(NSValue *)foo {
return [NSValue valueWithCGRect:CGRectMake(10, 10, 10, 10)];
}
-(void)callFoo {
CGRect rect = [[self foo] CGRectValue];
NSLog(@"%@",NSStringFromCGRect(rect));
}
and in my test case,
NSValue *rect = [NSValue valueWithCGRect:CGRectMake(0, 0, 0, 0)];
[[[master expect] andReturn:rect] foo];
update :
This looks like problem with the memory [but I am not sure, just a guess].
Mocked method will give me correct structure if I create a structure with 2 CGFloat
variable, whereas it will crash if I create the structure with 2 double
variable or 3 CGFloat
variable.
Interesting :)