1

The current implementation I have is an AFHTTPClient subclass and accompanying manager class that handles the connection with an API. As the app will be used in places where network connectivity can't be guaranteed, I'm wanting to write some unit tests around the code which handles a change in the network reachability.

I've implemented a setReachabilityStatusChangeBlock and want to make sure that this gets called and behaves in the correct way.

Is anyone familiar with simulating a change in network reachability to be used in unit testing? I'm currently using Kiwi for unit testing and OHHTTPStubs for simulating the data returned from the webservice.

What is the usual way of testing these situations?

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
Matt Delves
  • 1,585
  • 2
  • 13
  • 19

1 Answers1

0

I'm doing this myself with apple's unit test framework. I used a separate class for testing that does not use reachability, but can be messages by the test harness, to simulate network up or down notification. You may need to restructure your primary code to support this.

EDIT: if you have a class that handles Reachability now - you would create a new class of the same name, same methods/properties, and perhaps some new methods that allow an external agent to tell that special class to "pretend" that something changed. So instead of a real reachability event, that class has a call -(void) fakeNetworkStatus:(BOOL)nowUp that you use. So the code in the test harness is now interacting with the fake Reachability class (which is of course not included in the unit tests).

EDIT2: had some more thoughts on this. You can have duplicate implementation files, but Xcode does not really support multiple interface files. What I recently did was create a new configuration, ReleaseForUnitTest, where I defined "UNIT TESTING". Now, I could provide blocks of either code or method prototypes that were just for unit testing. Using this you can have two implementation files and one interface file.

David H
  • 40,852
  • 12
  • 92
  • 138
  • Good to hear that you're testing this in your app. Are you able to provide some code the demonstrates how you use a separate class for achieving the testing? – Matt Delves May 29 '13 at 21:58