Search code examples
iosobjective-cobjective-c-blocksocmock

Capturing Block passed into method when using OCMock


I'm looking to capture a block that is passed into a mock object. Here is the concrete function of class being mocked:

[self.imageFetcher fetchImageWithURL:url success:^(NSURL *successUrl, UIImage *image) {
       //Implementation Details here
}];

Here is the my attempt to capturing the block:

OCMExpect([imageFetcher fetchImageWithURL:urlForSuccess success:[OCMArg checkWithBlock:^(void(^myBlock)(NSURL *,UIImage *)){
        myBlock(urlForSuccess,[UIImage new]);
        return YES;
    }]]);

However, this doesn't seem to be working. How can I use the checkWithBlock to capture the block and be able to call that block with parameters I choose?


Solution

  • If you want to invoke the block that is passed to the mock, then you should use [OCMArg invokeBlock]. See section 2.6 in the documentation for details: http://ocmock.org/reference/#stubing-methods