How do I mock network requests in Xcode UI tests while the tests are running?
Asked Answered
E

3

4

We've got a suite of UI tests for our app written using KIF which I'd like to convert to use the new Xcode UI test framework.

The app is a client of a Rest AI whose responses we're currently mocking by using NSURLProtocol to serve predefined JSON files in response to the GETs, POSTs, PUTs, etc... All the tests are defined using the data in these files, so I want to continue using them. The same endpoints on the server return different data at different points in the tests, so I can't mock them up-front, I need to be able to call a method while the test is running to mock the server's next response.

Unfortunately, using NSURLProtocol inside an Xcode UI test doesn't affect the tested app, and I've only seen ways of sending data to the app via launch arguments or environment, such as in this answer. I need to mock them differently at different stages during my tests. How can I mock network requests from inside the UI test in a way that changes during the test? Or how can I communicate with the app being tested so I can get it to mock the requests itself?

Euphemie answered 21/4, 2016 at 11:56 Comment(0)
B
4

We've developed an embedded HTTP server in pure Swift, you can run the HTTP server with simple mock response handler in your test case, then pass the URL to the target app via environment variable like API_BASE_URL. I wrote an article about how to do it

Embedded web server for iOS UI testing

Basically there are two libraries, one is Embassy, it's the simple async HTTP server. Another one is Ambassador, a simple web framework for mocking API endpoints.

Bovine answered 28/6, 2016 at 21:18 Comment(0)
H
2

We have been facing the exact same problem trying to migrate from KIF to UI Tests. To overcome the limitations of UI Tests with regards to stubbing and mocking we built a custom link between the app and the test code by using a web server that is instantiate on the app. The test code sends HTTP requests to the app that get conveniently translated to a stubbing request (OHHTPStub), a NSUserDefault update, upload/download an item to/from the app. It's even possible to start monitoring network calls when you need to check that specific end points get called. It should be fairly simple to add new functionalities should you feel there's something missing.

Using the library is extremely simple, check it out on our github repo

Heterosexual answered 28/4, 2016 at 8:57 Comment(1)
We ran into exactly the same issue and have developed an embedded HTTP server for solving it. Please reference to my answer :PBovine
R
-1

You could either mock them using OHHTTPStubs, or by writing your own stubs.

In a nutshell, you should stub requests with a NSURLSession subclass and inject the stubbed NSURLSession into your networking layer. The launchEnvironment property might be useful to pass mocked urls to the test.

Raymonraymond answered 22/4, 2016 at 13:53 Comment(1)
OHHTTPStubs and NSURLSession aren't useful to me as my tests run in a different app to the app I wish to test. The launchEnvironment property isn't useful to me because I can't change it mid-test.Euphemie

© 2022 - 2024 — McMap. All rights reserved.