Mock for object implementing protocol

Discussion of the OCMock framework. If you have patches we would prefer you to send them to the mailing list, but attaching them to a topic is possible, too.

Mock for object implementing protocol

Postby tron_thomas » 16 Sep 2012, 03:51

I have an object I'm trying to test, and it is meant to accept an NSObject that implement a custom protocol. The object being testing will call methods from both the protocol and NSObject. Unfortunately the NSObject method called is not implemented in the NSObject protocol so inheriting the protocol from <NSObject> is not helpful.

I am unable to define a mock that I can use in the test.

When I use
Code: Select all
id mock = [OCMockObject mockForProtcol:@protocol(CustomProtocol)];
the test fails with the result [NSProxy doesNotRecognizeSelector] mentioning the method available in NSObject

When I use
Code: Select all
id mock = [OCMockObject mockForClass:[NSObject class]];
test fails with same kind of result only this time saying it doesn't recognize the method in the protocol.

How can I define a mock to test this object?
tron_thomas
 
Posts: 19
Joined: 04 Mar 2012, 02:14

Re: Mock for object implementing protocol

Postby eric » 17 Sep 2012, 23:36

Either mock the two portions separately, or create a dummy class that implements the protocol and mock that dummy class?
eric
 
Posts: 5
Joined: 14 Sep 2012, 22:25

Re: Mock for object implementing protocol

Postby tron_thomas » 18 Sep 2012, 05:49

I was able to get things to work by defining a category for NSObject that has the method of the protocol. I then created a stub implementation for the category method to the NSProxy method had something to call.

It was not obvious this would solve the problem. I wonder if the mock library could come up with something more obvious.
tron_thomas
 
Posts: 19
Joined: 04 Mar 2012, 02:14


Return to OCMock



cron