0

During an XCTestCase, I'd like to somehow say "disable internet" and it would effectively be disabled. Then later I'd like to somehow say "enable internet" and it would come back. The point is to see that my app behaves properly when network requests fail and then later succeed. What is the best way to do this?

Toggling airplane mode on and off could be sufficient, but I'm not aware of a way to do that. I've considered creating a dedicated simulator which is pre-configured to have the internet disabled, such as having airplane mode turned on, but that would limit my ability to run this test to one pre-configured simulator rather than the simulator (or device) of my choice.

John Bushnell
  • 1,851
  • 22
  • 29

1 Answers1

0

Since you're using NSURLSession directly, the least invasive solution is to hijack the HTTP and HTTPS protocols so you can control the success and content of URL requests.

There are libraries for this, but many might focus more on controlling response content than blackholing a request entirely by not responding or delaying the response.

Generally, you end up with a better factoring and a nearer, simpler seam if you push hitting the network to the other side of a protocol you control - and can readily provide a different implementation for when testing.

Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111