I'm currently working on a .net core solution (multiple projects) that uses Microsoft's app secrets management. I can pull secrets back in my runtime project (e.g. Console app), but when it comes to leveraging user secrets for integration tests in a separate project (e.g. Model.Test project), what's the right approach?
I can think of a few options:
Give each project the same UserSecretsId
: This seems like it'd made sense for test projects that may leverage the same secrets that the runtime project uses.
Let each project have unique UserSecretsId
: This would require that the secrets be manually kept in sync on the development machine
It seems like even between .net core 1.0 and 2.0 user secrets has changed somewhat and I don't have a lot of familiarity with this system in general. Thanks!
Setting up Unit Testing. The quickest way to set up unit testing for an ASP .NET Core web app project is to create a new test project using a template. This creates a cross-platform .NET Core project that includes one blank test. In Visual Studio 2019, search for “.net core test project” when creating a new project to identify test projects ...
First, Create a .NET core unit test project. Install-Package "Microsoft.Exensions.Configuration.UserSecrets" Similar to the setup in this article, right click on the project and add the following line. <PropertyGroup> <UserSecretsId> {test project identifier}- {guid}</UserSecretsId> </PropertyGroup>
To use user secrets, run the following command in the project directory: .NET Core CLI. dotnet user-secrets init. The preceding command adds a UserSecretsId element within a PropertyGroup of the project file. By default, the inner text of UserSecretsId is a GUID.
In a nutshell: a unit test is code you can write to test your application code. Your web application will not have any knowledge of your test project, but your test project will need to have a dependency of the app project that it’s testing.
I’m still learning about configuration in .NET Core 2 myself. When I set up my API project I shared the user secrets tag between the project and the unit test project by copying it between them. Meaning they both had the same user secrets guid, like your option 1. My rationale is that it would be the easiest way to maintain secrets between the two projects for the development team.
This article from Patrick Huber shows how to reference User Secrets in the config builder which was key for me - https://patrickhuber.github.io/2017/07/26/avoid-secrets-in-dot-net-core-tests.html
This article from Rick Strahl was also useful it shows how to configure a test helper in your unit tests for accessing the user secrets: https://weblog.west-wind.com/posts/2018/Feb/18/Accessing-Configuration-in-NET-Core-Test-Projects
Related implementation question - How to use user secrets in a dotnet core test project
Question related to referencing the config file - AppSettings.json for Integration Test in ASP.NET Core
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With