espressoexcess
espressoexcess

Reputation: 191

OCMock can't stub a category method

I'm trying to work with a mock of NSMutableURLConnection, but OCMock doesn't want to stub methods declared in the NSHTTPURLRequest category of NSMutableURLConnection.

id requestMock = OCMClassMock([NSMutableURLRequest class]);
OCMStub([requestMock setHTTPBody]);

The compiler won't accept the second line. I get "ARC Semantic Issue: No known instance method for selector 'setHTTPBody'."

The compiler allows me to stub HTTPBody (as opposed to setHTTPBody) but it never gets called.

Anyone know how to get a working stub and make the compiler happy in this case?

Upvotes: 3

Views: 1011

Answers (1)

Carl Lindberg
Carl Lindberg

Reputation: 2972

The method name is setHTTPBody: (with a colon at the end, which indicates an argument). The colon is an essential part of the method name. The compiler error is accurate; you need to use the correct method name.

You are using a nice class mock however, which will have an empty implementation for setHTTPBody: which won't do anything (will not store the data), so calling it won't help with other code which uses the accessor. You could try finding out which methods do get called by creating a non-nice mock, which will throw exceptions for any unexpected methods, and work your way down by trial and error to find the methods which really are needed. Or, perhaps create a regular NSMutableURLRequest object, and create a partial mock from that, only stubbing the methods you need to alter but leaving the rest of the behavior alone.

Upvotes: 5

Related Questions