4

I am using GHUnit & OCMock to do some testing work in my iOS app.

So I have some trouble integrating them.

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]; into

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

I got an error which says "Reason: 'NSCFString' should be equal to '255'. Should be equal"

I think the two statements should do exactly the same thing. Am I wrong?

leafduo
  • 144
  • 1
  • 5

1 Answers1

7

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

void (^theBlock)(NSInvocation *) = ^(NSInvocation *invocation) {
    NSString *capitalizedString = @"255";
    [invocation setReturnValue:&capitalizedString];
};
Christopher Pickslay
  • 17,523
  • 6
  • 79
  • 92
  • @leafduo `GHUnit` and `OCMock` are both not from Apple. – ThomasW Oct 22 '12 at 06:02
  • 3
    @ThomasW I think he's referring to the [NSInvocation docs](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSInvocation_Class/Reference/Reference.html#//apple_ref/doc/uid/20000212-setReturnValue_) – Christopher Pickslay Oct 22 '12 at 19:14