I\'ve been trying to intercept and stub/mock HTTP requests in Xcode 7 automated UI tests, using tools like OHHTTPStubs, with no luck.
Here\'s an example of how I am tryi
As Martijn correctly pointed out, because of how UI tests work, you cannot directly interact with the app at runtime, so any HTTP mocking or manipulation of things like NSUserDefaults
in a XCUITestCase
will not affect your app.
If you really need to be able to mock HTTP or setup & teardown your apps environment for specific UI tests, you will need to set launch arguments or launch environment variables before launching the app in the setUp()
method of a XCUITestCase
and then modify your app code to read the launch arguments or environment variables and bootstrap the test environment.
Example TestCase
class MyTestCase: XCTestCase {
/**
Called before each test in this test case.
*/
override func setUp() {
super.setUp()
let app = XCUIApplication()
app.launchArguments = [ "STUB_HTTP_ENDPOINTS" ]
app.launch()
}
}
Example AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
#if DEBUG
if (NSProcessInfo.processInfo().arguments.contains("STUB_HTTP_ENDPOINTS")) {
// setup HTTP stubs for tests
}
#endif
return true
}
Note: In order to use an HTTP mocking framework like OHHTTPStubs
in this example, the stubbing code and any JSON fixtures you need to use will all need to be in your app target, not the test target.
This is a very useful thread to read on the topic: https://github.com/AliSoftware/OHHTTPStubs/issues/124