gpt4 book ai didi

asp.net-core - 如何从单元测试访问 IApplicationEnvironment?

转载 作者:行者123 更新时间:2023-12-03 02:39:13 26 4
gpt4 key购买 nike

如何从 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.

关于asp.net-core - 如何从单元测试访问 IApplicationEnvironment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30111920/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com