我的MVC asp.net核心应用程序中有一个控制器。
在这个应用程序中,我使用DP来访问appsettings.json文件:
> LogNormalDistribution(logmean=-1.945322593396174, logstddev=0.968522285758599,
shift=0.2654438504801123)
现在我正在尝试为此创建一个测试脚本。如何从我的测试脚本访问/使用IConfiguration?
public class APIServicesController : Controller
{
IConfiguration _iconfiguration;
public APIServicesController(IConfiguration iconfiguration)
{
_iconfiguration = iconfiguration;
}
public IActionResult Index()
{
var Key = _iconfiguration.GetSection("EMAIL_API").GetSection("Key").Value;
var uri = _iconfiguration.GetSection("EMAIL_API").GetSection("uri").Value;
var emailAddress = "an email address";
var res = _api.ValidateEmail(emailAddress, Key , uri );
return View();
}
}
答案 0 :(得分:1)
我在集成测试课程中做了类似的事情:
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
var config = builder.Build();
现在有了配置文件,您可以获取所需的内容:
config.GetSection("whateverSection");
答案 1 :(得分:0)
一种在测试中完全构建内存的简单方法:
IConfiguration configuration = new ConfigurationBuilder()
.AddInMemoryCollection()
.Build();
configuration["SomeKey"] = "some value";