簡體   English   中英

.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"]

請注意,我在測試類的頂部具有以下代碼

public static IConfiguration InitConfiguration()
{
    var config = new ConfigurationBuilder()
        .AddJsonFile("appsettings.test.json")
        .Build();
    return config;
}

嘗試:

var inviteEndPointConfig = config["SendeInvite:InviteEndPoint"];

可能是因為您將屬性嵌套在SendeInvite中,所以您沒有獲得該值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM