久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C# 如何模擬 Configuration.GetSection("foo:bar")

C# how to mock Configuration.GetSection(quot;foo:barquot;).Getlt;Listlt;stringgt;gt;()(C# 如何模擬 Configuration.GetSection(foo:bar).Getlt;Listlt;stringgt;gt;())
本文介紹了C# 如何模擬 Configuration.GetSection("foo:bar").Get<List<string>>()的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我在 config.json 文件中有一個如下列表`

I have a list like following in config.json file `

{
  "foo": {
    "bar": [
      "1",
      "2",
      "3"
    ]
  }
}`

我可以使用

Configuration.GetSection("foo:bar").Get<List<string>>()

我想模擬 configuration.GetSection 來編寫單元測試.

I want to mock the configuration.GetSection to write unit test.

以下語法失敗

mockConfigRepo
    .SetupGet(x => x.GetSection("reportLanguageSettings:reportLanguageList").Get<List<string>>())
    .Returns(reportLanguages);

推薦答案

我遇到了同樣的問題,發(fā)現(xiàn)我需要為數(shù)組中的每個元素以及數(shù)組節(jié)點本身創(chuàng)建一個mock IConfigurationSection,然后設(shè)置父節(jié)點返回子節(jié)點,子節(jié)點返回它們的值.在 OP 示例中,它看起來像這樣:

I've encountered same issue and found that I needed to create a mock IConfigurationSection for every element in the array, as well as the array node itself, and then setup the parent node to return children, and children to return their values. In OP example, it would look like this:

        var oneSectionMock = new Mock<IConfigurationSection>();
        oneSectionMock.Setup(s => s.Value).Returns("1");
        var twoSectionMock = new Mock<IConfigurationSection>();
        twoSectionMock.Setup(s => s.Value).Returns("2");
        var fooBarSectionMock = new Mock<IConfigurationSection>();
        fooBarSectionMock.Setup(s => s.GetChildren()).Returns(new List<IConfigurationSection> { oneSectionMock.Object, twoSectionMock.Object });
        _configurationMock.Setup(c => c.GetSection("foo:bar")).Returns(fooBarSectionMock.Object);

附:我使用的是 Moq,所以請翻譯成您選擇的模擬庫.

P.S. I'm using Moq, so please translate to your mock library of choice.

附言如果您對為什么最終會起作用、不可修改的 Get() 方法的作用或有比 OP 更復(fù)雜的場景感興趣,閱讀此類可能會有所幫助:https://github.com/aspnet/Extensions/blob/release/2.1/src/Configuration/Config.Binder/src/ConfigurationBinder.cs

P.P.S. If you are interested in why this ends up working, what unmockable Get() method does, or have a more complex scenario than OP, reading this class may be helpful: https://github.com/aspnet/Extensions/blob/release/2.1/src/Configuration/Config.Binder/src/ConfigurationBinder.cs

這篇關(guān)于C# 如何模擬 Configuration.GetSection("foo:bar").Get&lt;List&lt;string&gt;&gt;()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進(jìn)行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權(quán)不起作用)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護(hù)進(jìn)程或服務(wù)器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發(fā)技
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調(diào)用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
Getting access token using email address and app password from oauth2/token(使用電子郵件地址和應(yīng)用程序密碼從 oauth2/token 獲取訪問令牌)
New Azure AD application doesn#39;t work until updated through management portal(新的 Azure AD 應(yīng)用程序在通過管理門戶更新之前無法運(yùn)行)
主站蜘蛛池模板: 国产成人精品免费视频大全最热 | 国产精品自产拍 | 老司机免费视频 | 91精品久久久久久久久久 | 欧美亚洲日本 | 亚洲精品中文字幕在线 | 日韩欧美三级电影在线观看 | 成人在线视频看看 | 久热免费 | 亚洲欧美日韩系列 | 精品在线一区二区三区 | 一级免费a | 色婷婷一区二区三区四区 | 精品影视| 91一区| 免费99精品国产自在在线 | 国产精品一区在线观看你懂的 | 欧美亚洲国产日韩 | 日韩一区二区在线看 | 日韩免费av | 综合五月婷 | 91精品国产乱码久久久久久 | av在线一区二区 | 精品欧美在线观看 | heyzo在线| 国产激情福利 | 国产视频一区二区 | 欧美一级小视频 | 国产ts人妖另类 | www.日本国产| 欧美一区永久视频免费观看 | 午夜男人免费视频 | 欧美极品在线观看 | 99热成人在线 | 亚洲精品v日韩精品 | 婷婷去俺也去 | 色婷婷av久久久久久久 | 91精品国产综合久久久久久 | 精品国产一区一区二区三亚瑟 | 在线超碰 | 一区欧美 |