How to access IApplicationEnvironment from unit test?

How can I access the IApplicationEnvironment from xUnit 2 unit test?

There are several scenarios in which I think I need this, including:

  • Reading from an inline configuration file inline with unit tests
  • Creating and writing unit tests related files
+4
source share
1 answer

Well, this is not ideal, but you can use a static services locator to get to it:

var appEnv = CallContextServiceLocator.Locator.ServiceProvider
    .GetService(typeof(IApplicationEnvironment)) as IApplicationEnvironment;

I'm not sure that xUnit introduces structure dependencies through the constructor, and I'm sure not. If this is the case (although that would be ideal), you can simply inject it into the test class through your constructor.

+4

All Articles