My Web application needs to read the Document DB keys from appsettings.json file. I have created a class with the key names and reading the Config section in ConfigureaServices()
as:
public Startup(IHostingEnvironment env) {
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services) {
services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
services.AddSession();
Helpers.GetConfigurationSettings(services, Configuration);
DIBuilder.AddDependency(services, Configuration);
}
I'm looking for the ways to read the Key values in Test project.
In the
project.json
from you test project, add the following dependencies:BancoSentencas
is the project I want to test. The other packages are from xUnit and the TestHost that will be our in-memory server.Include also this build option for the appsettings.json:
In my test project, I have the following test class:
And I also have the TestServerFixture class that will configure the in-memory server:
That's how I test my project. I use the Startup.cs from the main project, and I create a copy from the appsettings.json in my test project (appsettings.Development.json)
Honestly, if you are unit testing an application, you should try to isolate the class you are testing from all dependencies, like calling other classes, accessing file system, database, network etc. Unless you are doing integration testing or functional testing.
Having that said, to unit test the application, you probably want to mock these values from your appsettings.json file, and just test your logic.
So your
appsettings.json
would look like this.Then create a settings class.
Then register it in
ConfigureServices()
method.Then for example your controller/class could look like this.
Then in your tests project you can create such unit test class.
I used NSubstitute v2.0.0-rc for mocking.
This is based on the blog post Using Configuration files in .NET Core Unit Test Projects (written for .NET Core 1.0).
Create (or copy) the appsettings.test.json in the Integration test project root directory, and in properties specify "Build Action" as Content and "Copy if newer" to Output Directory. Note that it’s better to have file name (e.g.
appsettings.test.json
) different from normalappsettings.json
, because it is possible that a file from the main project override the file from the test project, if the same name will be used.Include the JSON Configuration file NuGet package (Microsoft.Extensions.Configuration.Json) if it's not included yet.
In the test project create a method,
Use the configuration as usual
BTW: You also may be interesting in reading the configuration into the IOptions class as described in Integration test with IOptions<> in .NET Core:
Suderson's solution worked for me when modified as below:
Copy the
appSettings.json
to your Test project root directory and mark its property as Content and Copy if newer.ConfigurationManager
is a class and it has a static propertyConfiguration
. This way the whole application can just access it asConfigurationManager.Configuration[<key>]