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
UI tests are ran in a separate instance from your application. While the classes from the application might be available to you, they are merely a copy.
In your application you can detect if you're running in UI testing mode with solutions provided here: How to detect if iOS app is running in UI Testing mode
I personally went with the launchEnvironment
solution mentioned in the original post; my setUp looks like this:
override func setUp() {
super.setUp()
let app = XCUIApplication()
app.launchEnvironment["TEST"] = "1"
app.launch()
}
And one of my singleton instantiators (called RealmManager
) looks like this (for instantiating a Realm database):
func realm() -> Realm {
let dic = NSProcessInfo.processInfo().environment
if dic["TEST"] != nil {
return try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "test"))
}
return try! Realm()
}
If you dislike the duplication, but you're probably already duplicating XCUIApplication().launch()
anyway, you can always make a custom test case class that extend XCTestCase
, override the setUp there with this addition and then use that in all your test classes.