如何从单元测试访问 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.
如何从 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.