Question

I would like to test a method which makes a GET request.

Here's a contrived example:

- (void)GET:(NSString *)URLString;

Rather than worry about the implementation details of this method (e.g. mocking, setting expectations, and then verifying a dependency), I'd prefer to just be able to test if the GET request was made.

I believe I can do this with NSURLProtocol but my attempts so far have been unsuccessful.

For example, using OCMock:

id mockURLProtocol = (NSURLProtocol *)[OCMockObject mockForClass:[NSURLProtocol class]];
[NSURLProtocol registerClass:mockURLProtocol];

[[mockURLProtocol expect] canInitWithRequest:[OCMArg any]];

[MYClass GET:@"test"];

[mockURLProtocol verify];
[mockURLProtocol stopMocking];
[NSURLProtocol unregisterClass:mockURLProtocol];

The above test fails since canInitWithRequest is never called.

How can I set expectations on NSURLProtocol to verify that the request is being made?

No correct solution

OTHER TIPS

Mocking NSURLProtocol doesn't make sense to me. NSURLProtocol is used in testing to create canned responses to certain requests, there is no need of mocking it (you don't want to test how the system interacts with it). This project makes use of NSURLProtocol, it may be useful for you in case you want to use this approach instead of a pure unit test where you test your NSURLConnectionDelegate implementation directly.

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