0

I have written a few xUnit tests for API controller methods. I am using the configuration setting from the azure portal app settings.

This is the code I used for initializing the settings.

private readonly  IConfiguration _configuration;
public   Controller(IConfiguration configuration)
{
    _configuration = configuration;
}

In the test case class,

IConfiguration _configuration;
[Fact]
public async void Method1()
{
    Controller controller = new Controller(_configuration);


}

After run the test I am getting an error

System.NullReferenceException : Object reference not set to an instance of an object.

What is the possible solution for this?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
D V V
  • 55
  • 5

1 Answers1

1

You need to define _configuration. At the moment you declare the reference, but don't assign an object to it. It is null.

You need somethine such as :

IConfiguration _configuration = new Configuration();

The above assumes, of course, that Configuration is a class that implements your IConfiguration interface.

Mark Willis
  • 818
  • 12
  • 19