How to capture the argument sent to the layout?

Does anyone know how to capture the argument sent to the OCMock object?

id mock = [OCMockObject mockForClass:someClass] NSObject* captureThisArgument; [[mock expect] foo:<captureThisArgument>] [mock foo:someThing] GHAssertEquals[captured, someThing, nil]; 

How can I check the foo argument? I am also happy to do this in the block in the definition of fake, but if I could get the object so that I could assert its function later, it would be brilliant.

Is this possible with OCMock?

+6
source share
2 answers

If you want to check your parameter, perhaps you can do it directly when you install your stub, for example:

 id mock = [OCMockObject mockForClass:someClass]; NSObject* captureThisArgument; [[mock expect] foo:[OCMArg checkWithBlock:^(id value){ // Capture argument here... }]]; 

Regards, Quentin A

+9
source

You can mute the call and pass it to the unit that checks it:

 NSObject *expected = ...; id mock = [OCMockObject mockForClass:someClass] void (^theBlock)(NSInvocation *) = ^(NSInvocation *invocation) { NSObject *actual; [invocation getArgument:&actual atIndex:2]; expect(actual).toEqual(expected); }; [[[mock stub] andDo:theBlock] foo:[OCMArg any]]; [mock foo:expected]; 

There is also a callback version, but the control flow is becoming more complex, as you need a state variable visible both for your test and for the validation callback:

 [[[mock stub] andCall:@selector(aMethod:) onObject:anObject] someMethod:someArgument] 
+3
source

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


All Articles