简体   繁体   中英

Using and injecting from appsettings.json in unit tests on .net core

I have created an asp.net core web application and a unit test application.

I created the asp.net application using the "ASP.NET Core Web Application (.NET Core)" template and created the unit test project using the "Class Library (.NET Core)" template.

I configured MSTest using the instructions on the following article:

Announcing MSTest Framework support for .NET Core RC2 / ASP.NET Core RC2

I have organised the application into Controllers and Services, the controllers read values from appsettings.json and pass these into the service methods as arguments.

I have an AppSettings class as follows

public class AppSettings
{
    public string Setting1 { get; set; }
    public string Setting2 { get; set; }
    public string Setting3etc { get; set; }
}

public static class App
{
    public static AppSettings Settings { get; set; }

    public static void ConfigureSettings(IOptions<AppSettings> settings)
    {
        Settings = settings.Value; 
    }
}

The controller constructor is as follows

public ValuesController(IOptions<AppSettings> settings)
{            
    App.ConfigureSettings(settings);
}

In Startup.cs I have the following line in the ConfigureServices method

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

I learned about this technique from the following article

Strongly Typed Configuration Settings in ASP.NET Core

This works at run time, but I'm having difficulties accessing these settings in my unit tests.

I'm trying to find a way to get the values from appsettings.json into my test project, so that I can pass them to the controllers and services.

My test methods look something like this

[TestMethod]
[TestCategory("Service - Method1")]
public void ServiceMethod1Test()
{
    // this compiles but gets null reference exception
    var setting1 = App.Settings.Setting1;
    var setting2 = App.Settings.Setting2;

    var service = new Service(setting1, setting2);

    var argument1 = "argument";
    var actual = service.Method1(argument1);
    var expected = "expected result";

    CollectionAssert.AreEqual(expected, actual);
}

[TestMethod]
[TestCategory("Controller - Method1")]
public void ControllerMethod1Test()
{
    // how do i create this settings instance?
    var settings = ???

    var controler = new ValuesController(settings);

    var argument1 = "argument";

    var actual = controller.Method1(argument1);
    var expected = "expected result";

    CollectionAssert.AreEqual(expected, actual);
}

How could I go about passing an instance of a class that implements IOptions<MySettings> to the controller constructor for controller tests, and how could I go about getting those values to pass to service methods in the service tests.

Just reference the Options package and use

IOptions<MyOptions> options = Options.Create(new MyOptions()
{
    ...
});

See here for source code reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM