-
Notifications
You must be signed in to change notification settings - Fork 56
Accessing the appsettings.json file
It is sometimes useful to have somewhere you can put variable data for use in your unit tests. EfCore.TestSupport has a small static class called AppSettings
which mainly contains connection strings, but you can use it for other data as well.
It works in the same way as the ASP.NET Core appsettings.json file (EfCore.TestSupport uses the same code).
NOTE: the appsettings.json file is optional, but the Creating connections strings feature relies on it.
The method AppSettings.GetConfiguration()
will get the configuration file using the ASP.NET Core code.
You can place any setting for your unit tests. There are two variants:
-
AppSettings.GetConfiguration(string settingsFilename = "appsettings.json")
This loads the configuration from theappsettings.json
file in the unit test project. It doesn't fail if there is no file there (that is on purpose, becauseCompareEfSql
calls it to see if there is a connection). This version allows you to define a different named .json file via thesettingsFilename:
parameter. For instance the code below would get a database connection string in the appsetting.json file in the project that the code is run in (typically your unit test project).
var config = AppSettings.GetConfiguration();
var connectionString = config .GetConnectionString("DefaultConnection");
-
AppSettings.GetConfiguration(string relativeToCallingAssembly, string settingsFilename = "appsettings.json")
This allows you to access a json configuration file in another directory. The base directory is the calling's assembly top level directory, e.g. "Test". You can add relative directory reference, e.g. "../SomeOtherProject/". For instance the code below would get the database connection string from the appsettings.json file in the projectMyAspNetCoreApp
.
var mainConfig = AppSettings.GetConfiguration("../MyAspNetCoreApp/");
var connectionString = mainConfig.GetConnectionString("DefaultConnection");
Click here
for an example of the appsettings.json
file.
Click here for examples of the different usages.
If you just want to read a connection string from the appsettings.json file then you can use the
GetConnectionString
method, with the name of the connection string you want as the other parameter.
var config = AppSettings.GetConfiguration();
var connectionString = config .GetConnectionString("DefaultConnection");
Just like ASP.NET Core you can any data from the appsettings.json file. Here is an example json file
{
"ConnectionStrings": {
"UnitTestConnection": "Server=(localdb)\\mssqllocaldb;Database=EfCore.TestSupport-Test;Trusted_Connection=True;MultipleActiveResultSets=true",
},
"MyInt": 1,
"MyObject": {
"MyInnerInt": 2
}
}
And here is how to read the non-connection string values
var config = AppSettings.GetConfiguration();
var myInt = config["MyInt"]; //value returned is "1"
var myInnerInt = config["MyObject:MyInnerInt"]; //value returns is "2"
- Testing against a PostgreSQL db
- Changes in EfCore.TestSupport 5
- Testing with production data
- Using an in-memory database (old)