### Features
-
[OCMArg invokeBlock]- A constraint specific to block arguments.
- It auto-invokes the block with default values for each of its arguments.
- E.g.
OCMStub([uiView animateWithDuration:0.4 animations:[OCMArg invokeBlock] completion:[OCMArg invokeBlock]]).
-
[OCMArg invokeBlockWithArgs:...]- A constraint specific to block arguments.
- It auto-invokes the block with the given values for each of its arguments.
- The number of arguments passed in the vargs must match the number of arguments that the block takes.
- The type of the arguments passed in the vargs must be compatible with the types of the arguments that the block takes.
- Argument values which are not Objective-C objects (e.g. primitives, structs, pointers) must be boxed in
NSValue. - Use
OCMOCK_VALUEto do this conveniently. - Pass
OCMDefaultin the vargs to invoke the block with a default argument. - List of arguments must be nil-terminated.
- Note that it requires additional parentheses when used in an
OCMStubmacro to avoid the vargs being parsed as separate macro arguments. - E.g.
OCMStub([obj doSomethingComplexCompletionBlock:([OCMArg invokeBlockWithArgs:@"A first param", @123, OCMOCK_VALUE(somePtr), OCMDefault, nil])])
### Use Cases
UIViewControllertransition methodsUIViewanimation methods- Third party dependencies, e.g.
RestKit NSArrayenumeration methods
-
This repo contains 2 test harnesses:
Strange Types, which tests these features out with all sorts of obscure block signatures,- A sample application taken from BetweenKit, which aims to demonstrate how this feature might be used in the real world.
-
All tests have passed on the following:
- iPhone 6, iOS 8.3
- iPad Mini, iOS 8.1
- iPad 2, iOS 8.3
- iPad 2 Simulator, iOS 7.1, iOS 8.1, iOS 8.4
- iPad Retina Simulator, iOS 7.1, iOS 8.1, iOS 8.4
-
Tests in my OCMock fork have passed on the following:
- Macbook Pro, Retina 15", OS X Yosemite 10.10.4
### Limitations
- This relies on
NSInvocationto invoke the blocks and as a result, does not supportva_lists orunionsas arguments. - This has not been tested with function pointers as block arguments.
OCMBlockArgCalleris quite strict: it will throw if the type signature of a given argument doesn't match the type signature of the block (to offer as much feedback as possible to the user).
### References