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.
- UIStatusBar as in Facebook new iOS7 application
- Play Video in Background using Avplayer
- implementing Queue Services in AV Audio Recorder using Swift
- Distribution provisioning profile still expire even after clicking the renew button
- Recording mp3 instead of caf file
- Is there any value in using core data for iPhone apps?
2 Solutions Collect From Internet About “Mock API Requests Xcode 7 Swift Automated UI Testing”
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.
If you are worried about the idea of mocks making it into a production environment for any reason, you can consider using a 3rd party solution like Charles Proxy.
Using the map local tool you can route calls from a specific endpoint to a local file on your machine. You can past plain text in your local file containing the response you want it to return. Per your example:
Your login hits endpoint yoursite.com/login
in Charles you using the map local tool you can route the calls hitting that endpoint to a file saved on your computer i.e mappedlocal.txt
mappedlocal.txt contains the following text
HTTP/1.1 404 Failed
When Charles is running and you hit this endpoint your response will come back with a 404 error.
You can also use another option in Charles called “map remote” and build an entire mock server which can handle calls and responses as you wish. This may not be exactly what you are looking for, but its an option that may help others, and its one I use myself.
- Body and Face Detection Issue
- Clean Architecture – Robert Martin – How to connect use cases
- Autolayout Error with UITableView
- TabBarController and SplitViewController
- Apple Mach-O Linker Warning – Framework Search Paths?
- Xcode – build phase or script for copying only modified files to App Bundle
- how to open app store with in our ipad or iphone application?
- How to disable the “drag down to view Notifications” feature?
- iOS 8+ framework with nested embedded framework
- Unable to execute tests from command line via xcodebuild
- NSImage Getting Resized when I draw Text on it
- Check if username already exist's : Swift, Firebase
- Fade out Playback Volume on MPMusicPlayerController on iOS 5
- Building XMLRPC.framework: Wrong Architecture?
- how to get current location lat long in ios 8?