-1

I am writing unit tests for my .Net Core 5 web application. In the Startup.cs, I have 4 different calls similar to the following:

services.Configure<ServicesSettings>(Configuration.GetSection("ServicesSettings"));

each injecting a different class. I have tried the following:

ServicesSettings _serviceSettings = services.BuildServiceProvider().GetService<ServicesSettings>();
Assert.NotNull(_serviceSettings);

but it returns null. I am surprised that I could not find a similar question. I tried to look at ServiceCollection, but could not find a method.

Phil Huhn
  • 3,307
  • 3
  • 18
  • 35
  • Its not clear what you are trying to achieve. How to use configuration in a netcore unit test project or unit testing netcore configuration it self ? https://stackoverflow.com/questions/39131219/is-it-possible-to-use-dependency-injection-with-xunit ? – edelwater Dec 29 '21 at 03:11
  • Are you unit testing startup.cs code? – Chetan Dec 29 '21 at 03:24

1 Answers1

3

get service for IOptions

IOptions<ServicesSettings> _serviceSettingOption = services.BuildServiceProvider().GetService<IOptions<ServicesSettings>>();
_serviceSetting = _serviceSettingOption.Value;

Assert.NotNull(_serviceSetting);

I believe you are unit testing config injection capability/functionality, which is not required

Akshay
  • 96
  • 4