In our project there's a class with a bunch of static fields that is used to hold setting values in memory:
public class MyConfigurationSettings
{
public static string MySettingId { get; set; }
public string MySettingProcessIdentifier
{
get { return MySettingId; }
set { MySettingId = value; }
}
}
I'm not sure why it's like this - that's lost to the mists of time. This object is populated from a settings file in startup.
var defaultConfiguration = new MyConfigurationSettings();
configuration.GetSection("MyConfiguration").Bind(defaultConfiguration);
However, I now need to write a unit test that's dependent on these values. The object is available to my tests:
TypeId = MyConfigurationSettings.MySettingId
But the value is always empty (or zero for an int) and most of the tests need to check which setting value the code is using.
I had a look at using ConfigurationBuilder but the only useful option seemed to be to load a json file, which isn't really ok for a unit test, and I'm not sure how to bind it.
How can I populate this object for a unit test?
When your project starts, there should be a root defined somewhere, which tells the app where to look for appsettings.json (I assume you use it, as long as you are talking about configuration). E.g.:
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
When you run the tests your execution directory is different and file is not loaded. So you can:
You register your settings in Startup:
services.Configure<MySettings>(this.Configuration.GetSection("MySettings"));
In you class in constructor use DI:
public class MyClass{
public MyClass(IOptions<MySettings> settings) { ... }
}
In your test:
new MyClass(Options.Create<MySettings>(new MySettings(/* test settings here */)))