Skip to main content
Improved formatting, punctuation.
Source Link
Pang
  • 10.2k
  • 146
  • 87
  • 126

Agree with Aleha that using a testSettings.json configuration file is probably better. And

And then, instead of injecting the IOptionIOption<SampleOptions>, you can simply inject the real SampleOptions in your class constructor, when unit test the class, you can do the following in a fixture or again just in the test class constructor:

 var builder = new ConfigurationBuilder() .AddJsonFile("testSettings.json", true, true) .AddEnvironmentVariables(); var configurationRoot = builder.Build(); configurationRoot.GetSection("SampleRepo").Bind(_sampleRepo); 
var builder = new ConfigurationBuilder() .AddJsonFile("testSettings.json", true, true) .AddEnvironmentVariables(); var configurationRoot = builder.Build(); configurationRoot.GetSection("SampleRepo").Bind(_sampleRepo); 

Agree with Aleha that using a testSettings.json configuration file is probably better. And then instead of injecting the IOption you can simply inject the real SampleOptions in your class constructor, when unit test the class, you can do the following in a fixture or again just in the test class constructor:

 var builder = new ConfigurationBuilder() .AddJsonFile("testSettings.json", true, true) .AddEnvironmentVariables(); var configurationRoot = builder.Build(); configurationRoot.GetSection("SampleRepo").Bind(_sampleRepo); 

Agree with Aleha that using a testSettings.json configuration file is probably better.

And then, instead of injecting the IOption<SampleOptions>, you can simply inject the real SampleOptions in your class constructor, when unit test the class, you can do the following in a fixture or again just in the test class constructor:

var builder = new ConfigurationBuilder() .AddJsonFile("testSettings.json", true, true) .AddEnvironmentVariables(); var configurationRoot = builder.Build(); configurationRoot.GetSection("SampleRepo").Bind(_sampleRepo); 
Source Link

Agree with Aleha that using a testSettings.json configuration file is probably better. And then instead of injecting the IOption you can simply inject the real SampleOptions in your class constructor, when unit test the class, you can do the following in a fixture or again just in the test class constructor:

 var builder = new ConfigurationBuilder() .AddJsonFile("testSettings.json", true, true) .AddEnvironmentVariables(); var configurationRoot = builder.Build(); configurationRoot.GetSection("SampleRepo").Bind(_sampleRepo);