Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock API Requests Xcode 7 Swift Automated UI Testing

Is there a way to mock requests when writing automated UI tests in Swift 2.0. As far as I am aware the UI tests should be independent of other functionality. Is there a way to mock the response from server requests in order to test the behaviour of the UI dependant on the response. For example, if the server is down, the UI tests should still run. Quick example, for login, mock if password failed then UI should show alert, however, if the login is successful the next page should be shown.

like image 484
Liam Avatar asked Oct 08 '15 09:10

Liam


1 Answers

In its current implementation, this is not directly possible with UI Testing. The only interface the framework has directly to the code is through it's launch arguments/environment.

You can have the app look for a specific key or value in this context and switch up some functionality. For example, if the MOCK_REQUESTS key is set, inject a MockableHTTPClient instead of the real HTTPClient in your networking layer. I wrote about setting the parameters and NSHipster has an article on how to read them.

While not ideal, it is technically possible to accomplish what you are looking for with some legwork.

Here's a tutorial on stubbing network data for UI Testing I put together. It walks you through all of the steps you need to get this up and running.

like image 163
Joe Masilotti Avatar answered Nov 15 '22 22:11

Joe Masilotti