2

I've spent all afternoon trawling the web for tutorials and examples of using OCMock but I've still no idea if its possible and if so how to use it in combination with NSURLConnection to set up test code to simulate a server sending data in response to HTTP POSTs or GETs.

Does anybody know of a handy tutorial for doing this?

UPDATE: I subsequently came across this: how to unit test a NSURLConnection Delegate?

Community
  • 1
  • 1
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • It depends a bit on how you're making the request. Are you implementing `NSURLConnectionDelegate`? Or are you using one of the send... class methods? Or are you doing something else? – Christopher Pickslay May 09 '13 at 17:41
  • Yes, however I've managed to find this previous posting which does why I want.http://stackoverflow.com/questions/9908547/how-to-unit-test-a-nsurlconnection-delegate. However all the stuff I've seen about OCMock, including this link, requires that APIs be public for it to work. I tried the example in the link making newAsynchronousRequest non public, as it should be, but the test code then can't see it. So OCMock is useful but it seems you have to make parts of your API public that may otherwise be private – Gruntcakes May 09 '13 at 20:17
  • You don't have to put the method under test in your header. Just declare it in a class extension in your test class. – Christopher Pickslay May 11 '13 at 15:57

1 Answers1

4

Yes, but I've found another package, OHHTTPStubs, to be a LOT easier for doing exactly what you're looking for:

https://github.com/AliSoftware/OHHTTPStubs

[OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) {
    return [request.URL.path isEqualToString:@"/api/oauth/access_token"];
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
    return [OHHTTPStubsResponse responseWithFile:@"auth_login_passing.json" contentType:@"text/json" responseTime:OHHTTPStubsDownloadSpeedWifi];
}];
Paul Armstrong
  • 7,008
  • 1
  • 22
  • 36
  • This looks interesting and I've played around with it, however it doesn't seem possible, or straightforward, to chain together a sequence of responses. For example suppose I want to emulate a comms conversation. I need to set up the stub to first return packet a, then packet b and so on. But the stub gets called multiple times for a single call to NSURLConnection:estabishConnectionUsingRequest:. (3 or 4 times). So its difficult to determine when to send package b or packet c etc. – Gruntcakes May 09 '13 at 19:16
  • @Paul Armstrong glad my lib solved your problem :) — @Mungbeans don't hesitate to propose a Feature Request on my GitHub in you see some interesting feature missing. But in your case, I don't see a problem to do what you need: for example you can declare a `static int responseCount = 0` variable right before the `[OHHTTPStubs stubRequestsPassingTest:withStubResponse:]` call, and in the 2nd block return the appropriate response according to the value of `responseCount` then increment this variable for the next iteration. – AliSoftware Oct 21 '13 at 16:07
  • 1
    Additional note: `OHHTTPStubs` has greatly evolved since this answer and is now in version `3.0.2` which provides a more cleaner API, support for NSURLSessions, and more, so I suggest you migrate to this new version ;) ([See release notes here](https://github.com/AliSoftware/OHHTTPStubs/releases)) – AliSoftware Oct 21 '13 at 16:09