簡體   English   中英

.NET Core 2.0 用戶機密和單元測試

[英].NET Core 2.0 User Secrets and Unit Testing

我目前正在研究使用Microsoft 的應用機密管理的 .net 核心解決方案(多個項目)。 我可以在我的運行時項目(例如控制台應用程序)中提取秘密,但是當涉及在單獨的項目(例如 Model.Test 項目)中利用用戶秘密進行集成測試時,正確的方法是什么?

我能想到幾個選擇:

  1. 為每個項目提供相同的UserSecretsId :這對於可能利用運行時項目使用的相同機密的測試項目似乎很有意義。

  2. 讓每個項目都有唯一的UserSecretsId :這需要在開發機器上手動保持秘密同步

似乎即使在 .net 核心 1.0 和 2.0 用戶機密之間也發生了一些變化,而且我對這個系統一般不太熟悉。 謝謝!

我本人仍在學習有關.NET Core 2中的配置的信息。 設置API項目時,我通過在項目和單元測試項目之間復制用戶密鑰來共享它們。 意味着它們都具有相同的用戶機密guid,就像您的選項1一樣。我的理由是,對於開發團隊來說,這是在兩個項目之間維護機密的最簡單方法。

Patrick Huber的這篇文章展示了如何在配置構建器中引用用戶機密,這對我來說很關鍵-https: //patrickhuber.github.io/2017/07/26/avoid-secrets-in-dot-net-core-tests .html

Rick Strahl的這篇文章也很有用,它說明了如何在單元測試中配置測試助手來訪問用戶機密: https : //weblog.west-wind.com/posts/2018/Feb/18/Accessing-Configuration- NET核心測試項目

相關實施問題- 如何在dotnet核心測試項目中使用用戶機密

與引用配置文件-AppSettings.json有關的問題, 用於ASP.NET Core中的集成測試

這提供了一個具體的例子,使用Dale C 之前提供給這個問題的參考資料。

它已經過 .NET6 測試。 該示例將從

  • appsettings.json
  • 秘密.json
  • 環境變量

創建一個測試項目

在您的測試項目中

  1. 添加 nuget package Microsoft.Extensions.Configuration.UserSecrets
  2. 在我們測試項目的.csproj文件中,添加我們要訪問的用戶密碼的 guid。
<PropertyGroup>
    <UserSecretsId>0e8ea027-2feb-4098-ba69-4a6711e8eee2</UserSecretsId>
</PropertyGroup>
  1. 創建測試 class
using NUnit.Framework;
using MediatR;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;


namespace Example
{
    public class ConfigurationExampleTest
    {
        public IMediator? _mediator { get; set; }
        public IConfiguration config { get; set; }

        [SetUp]
        public void Setup()
        {            
            // The startupPath is simply one way of getting the path to
            // appettings.json. We could hard code tha path if necessary
            // or use any other method to obtain the path to appsettings.json
            var filepath = typeof(WebApp.Startup)
                .Assembly.Location;
            var StartupPath = Path.GetDirectoryName(filepath);

            config = new ConfigurationBuilder()
                .SetBasePath(StartupPath)
                .AddJsonFile("appsettings.json", optional: true)
                .AddUserSecrets<ConfigurationExampleTest>()
                .AddEnvironmentVariables()
                .Build();

           
            var host = Host.CreateDefaultBuilder()
                .ConfigureWebHostDefaults(builder =>
                {
                    builder.UseStartup<WebApp.Startup>();
                })
                .ConfigureServices(services =>
                {
                    // Adds the config to the dependency injection
                    // This makes the config object accessible in
                    // your WebApp/project-under-test
                    services.AddSingleton(config);
                })
                .Build();
             
             // This will get any service we added to the dependency injection
             // in WebApp.Startup
            _mediator = host.Services.GetService<IMediator>();
        }

        [TearDown]
        public void Teardown()
        { }

        [Test]
        public void ExampleTest()
        {            
            // The configuration object will now be available
            // vai dependency ibjection

            // We can use the_mediator instance we got from the
            // dependency injection here
            
            // _mediator.Send(<your request>);
            
        }
    }
}

參考:

暫無
暫無

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

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