I have the following code in my app. Its behaviour can be altered by setting a "MY_KEY" key in its environment dictionary of its process information.
func myMethod() {
var environment = NSProcessInfo.processInfo().environment
if environment["MY_KEY"] { /* do something /* }
}
I would like to test this in a unit test. The problem is that changing the environment dictionary in the unit test does no affect the dictionary in the app.
class MyAppTests: XCTestCase {
func testMe() {
var environment = NSProcessInfo.processInfo().environment
environment["MY_KEY"] = "my value"
myMethod()
// The app's environment does not change
}
end
Is it possible to change the environment dictionary of the app from a unit test?
The environment provided by NSProcessInfo is read-only. You can set environment variable using setenv
c function (works fine from Swift), like this:
setenv("MY_KEY", "my value", 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With