gpt4 book ai didi

c# - 如何在 dotnet core 测试项目中使用用户 secret

转载 作者:行者123 更新时间:2023-12-01 23:54:32 24 4
gpt4 key购买 nike

我想将集成测试的数据库连接字符串存储为用户 secret 。我的project.json 如下所示:

{
...

"dependencies": {
...
"Microsoft.Extensions.Configuration.UserSecrets": "1.1.0"
},

"tools": {
"Microsoft.Extensions.SecretManager.Tools": "1.1.0-preview4-final"
},

"userSecretsId": "dc5b4f9c-8b0e-4b99-9813-c86ce80c39e6"
}

我已将以下内容添加到测试类的构造函数中:

IConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddUserSecrets();

但是,当我运行测试时,遇到该行时会引发以下异常:

An exception of type 'System.InvalidOperationException' occurred in Microsoft.Extensions.Configuration.UserSecrets.dll but was not handled in user code

Additional information: Could not find 'UserSecretsIdAttribute' on assembly 'dotnet-test-nunit, Version=3.4.0.0, Culture=neutral, PublicKeyToken=null'.

我是否错过了某些内容或者我想要的内容不受支持?

最佳答案

请参阅 https://patrickhuber.github.io/2017/07/26/avoid-secrets-in-dot-net-core-tests.html 中的说明,特别是在InitialiseTest中添加

// the type specified here is just so the secrets library can 
// find the UserSecretId we added in the csproj file
var builder = new ConfigurationBuilder()
.AddUserSecrets<HttpClientTests>();

Configuration = builder.Build()

但是请注意,它不允许在构建服务器上运行测试

关于c# - 如何在 dotnet core 测试项目中使用用户 secret ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41941503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com