Using Block Expectations with OCMock

I use GHUnit and OCMock to run some tests in my iOS application.

I have some problems with their integration.

The following code works well.

NSString *s = [NSString stringWithString:@"122"]; id mock = [OCMockObject partialMockForObject:s]; [[[mock stub] andReturn:@"255"] capitalizedString]; NSString *returnValue = [mock capitalizedString]; GHAssertEqualObjects(returnValue, @"255", @"Should be equal"); [mock verify]; 

But when I change [[[mock stub] andReturn: @ "255"] capitalizedString]; in

 [[[mock stub] andDo:^(NSInvocation *invocation) { [invocation setReturnValue:@"255"]; }] capitalizedString]; 

I got the error "Reason:" NSCFString "must be equal to" 255 ". Must be equal to"

I think the two statements should do the same. I'm wrong?

+4
source share
1 answer

setReturnValue: expects a pointer to the return value, so your block should be:

 void (^theBlock)(NSInvocation *) = ^(NSInvocation *invocation) { NSString *capitalizedString = @"255"; [invocation setReturnValue:&capitalizedString]; }; 
+7
source

Source: https://habr.com/ru/post/1345343/


All Articles