1

I'd like to dynamically return a value dependent of a parameter to a mocked method, conceptionally like this:

[realObject stub] myMethod:CAPTUREDARGUMENT) andReturn:myMethod:CAPTUREDARGUMENT];

Or access the invocation in a block like with OCMock:

void (^theBlock)(NSInvocation *) = ^(NSInvocation *invocation) {
    /* code that reads and modifies the invocation object */
};
[[[mock stub] andDo:theBlock] someMethod:[OCMArg any]];

Is that possible with Kiwi?

fabb
  • 11,660
  • 13
  • 67
  • 111

2 Answers2

5

It is possible using stub:withBlock::

[realObject stub:@selector(myMethod:) withBlock:^id(NSArray *params) {
    return [params objectAtIndex:0];
];
fabb
  • 11,660
  • 13
  • 67
  • 111
0

The recommended way to capture arguments is using a capture spy, e.g.:

id testDouble = [SomeClass mock];
object.property = testDouble;

KWCaptureSpy *spy = [testDouble captureArgument:@selector(methodWithParam:) atIndex:0];

[object doSomethingWithProperty];

[[spy.argument should] equal:someResult];

It can also be achieved using stub:withBlock:, but capture spies tend to be make your intention clearer when it comes to the task of inspecting method arguments. This makes for more readable specs.

Adam Sharp
  • 3,618
  • 25
  • 29
  • I do not want to capture an argument. I want to stub a method of a real object, but not return a constant value, but one dependent of a passed argument. – fabb Nov 26 '13 at 13:44
  • @fabb Could you update the question to make this clearer? The way it reads, especially given the title, it sounds like you just need to capture an argument, not change it or return a different value. – Adam Sharp Jan 06 '14 at 19:53
  • is it better like this? i think the code example was already clear, no? – fabb Jan 07 '14 at 12:46