如何从单元测试访问 IApplicationEnvironment?

How do I access the IApplicationEnvironment from a unit test?

如何从 xUnit 2 单元测试访问 IApplicationEnvironment?

有几种情况我认为我需要这个,包括:

嗯,这并不理想,但您可以使用 the static service locator 来实现它:

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

I am not sure if xUnit injects framework dependencies in through the constructor and I bet it doesn't. If it does though (which would be perfect), you can just inject it into the test class through its constructor.