Question

I have a method that takes a string and a completion block argument. I only care about the string argument, but OCMockObject throws an exception, what should I pass as the block argument?

My Protocol

@protocol SomeService <NSObject>

- (void)fetchDataForUsername:(NSString *)username andCompletion:(void (^)(NSArray *someData, NSError *error))completion;

@end

My test

OCMockObject *mock = [OCMockObject niceMockForProtocol:@protocol(SomeService)];
[[mock expect] fetchDataForUsername:@"SPECIFIC_USERNAME" andCompletion:[OCMArg any]];

Error Log

**-[OCMAnyConstraint copyWithZone:]: unrecognized selector sent to instance 0xdc79750**
Was it helpful?

Solution

I had some problems with mocking protocols as well. In the general case, OCMock is happy to handle blocks arguments:

// Foo
+ (void)blockTest
{
    [UIView animateWithDuration:10.0 animations:^{
        [[[[UIApplication sharedApplication] windows][0] rootViewController] view].alpha = 0.5;
    }];
}

// Test -- this works fine!
- (void)testBlock
{
    id viewMock = [OCMockObject mockForClass:UIView.class];
    [[viewMock expect] animateWithDuration:10.0 animations:OCMOCK_ANY];

    [Foo blockTest];
    [viewMock verify];

}

To get around issues with protocol mocking, I create a dummy class that implements the protocol (with empty methods), then mock the methods of this class and use it like any other mock object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top