.NET Core 获取配置 appsettings.json 值
.NET Core getting Configuration appsettings.json values
我正在尝试为以下内容编写单元测试:
[TestMethod]
public void GetInviteEndPoint_ShouldAccessAppSettings()
{
//Data pulled from the appsettings.test.json
var config = InitConfiguration();
var inviteEndPointConfig = config["InviteEndPoint"]; // <-- Pain Point
//Arrange Test && Mock if needed
string mockInviteEndPoint = "https://graph.microsoft.com/v1.0/invitations";
//Actual Code from Application (ACT)
SendInvite sendInvite = new SendInvite();
string inviteEndPoint = sendInvite.GetInviteEndPoint(config);
//Assert
// Assert always tests (Expected[Arranged], Actual[From Code Base])
Assert.AreEqual(mockInviteEndPoint, inviteEndPoint);
}
我的 appsettings.json 和 appsettings.test.json 看起来一模一样。我很难从 .json 文件中获取值。我想知道是否有人可以提供我所坚持的这段代码的任何见解。
{
"SendeInvite": {
"InviteEndPoint": "https://graph.microsoft.com/v1.0/invitations"
...Code Omitted...
}
}
我是不是叫错了config["InvitedEndPoint"]
?
请注意我在测试的顶部有以下代码Class
public static IConfiguration InitConfiguration()
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.test.json")
.Build();
return config;
}
尝试:
var inviteEndPointConfig = config["SendeInvite:InviteEndPoint"];
可能是因为您在 SendeInvite 中嵌套了属性,所以您没有获得值。
我正在尝试为以下内容编写单元测试:
[TestMethod]
public void GetInviteEndPoint_ShouldAccessAppSettings()
{
//Data pulled from the appsettings.test.json
var config = InitConfiguration();
var inviteEndPointConfig = config["InviteEndPoint"]; // <-- Pain Point
//Arrange Test && Mock if needed
string mockInviteEndPoint = "https://graph.microsoft.com/v1.0/invitations";
//Actual Code from Application (ACT)
SendInvite sendInvite = new SendInvite();
string inviteEndPoint = sendInvite.GetInviteEndPoint(config);
//Assert
// Assert always tests (Expected[Arranged], Actual[From Code Base])
Assert.AreEqual(mockInviteEndPoint, inviteEndPoint);
}
我的 appsettings.json 和 appsettings.test.json 看起来一模一样。我很难从 .json 文件中获取值。我想知道是否有人可以提供我所坚持的这段代码的任何见解。
{
"SendeInvite": {
"InviteEndPoint": "https://graph.microsoft.com/v1.0/invitations"
...Code Omitted...
}
}
我是不是叫错了config["InvitedEndPoint"]
?
请注意我在测试的顶部有以下代码Class
public static IConfiguration InitConfiguration()
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.test.json")
.Build();
return config;
}
尝试:
var inviteEndPointConfig = config["SendeInvite:InviteEndPoint"];
可能是因为您在 SendeInvite 中嵌套了属性,所以您没有获得值。