How to change application dictionary NSProcessInfo application from unit test?

I have the following code in my application. Its behavior can be changed by setting the "MY_KEY" key in its dictionary of its process information environment.

func myMethod() {
  var environment = NSProcessInfo.processInfo().environment
  if environment["MY_KEY"] { /* do something /* }
}

I would like to check this in unit test. The problem is that changing the environment dictionary in the unit test does not affect the dictionary in the application.

class MyAppTests: XCTestCase {
  func testMe() {
    var environment = NSProcessInfo.processInfo().environment
    environment["MY_KEY"] = "my value"
    myMethod()
    // The app environment does not change
  }
end

Is it possible to change the application environment dictionary with unit test?

+4
source share
1 answer

The environment provided by NSProcessInfo is read-only. You can set the environment variable using the setenvc function (works fine from Swift), for example:

setenv("MY_KEY", "my value", 1)
+5
source

Source: https://habr.com/ru/post/1611701/


All Articles