我正在根据https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options使用IOptions<>

我有以下 Controller 。

using Microsoft.Extensions.Options;
public class MyController : Controller
{
    private readonly ILogger<SourceController> _logger;
    private readonly MyOptions _option;

    public SourceController(ILogger<SourceController> logger, IOptions<SyncOptions> optionAccessor)
    {
        _logger = logger;
        _option = optionAccessor.Value;
    }

现在,我正在创建一个单元测试设置。如何模拟IOptions()?
MyTest() { // Test constructor
    var loggerFactory = new LoggerFactory().AddConsole(LogLevel.Information).AddDebug().AddNLog();
    loggerFactory.ConfigureNLog("nlog.config");
    logger = loggerFactory.CreateLogger<MyController>();

    option = new Options<MyOptions>(); // Error! How to mock IOptions<MyOptions>()?

    app = new MyController(logger, option);

最佳答案

使用助手类Microsoft.Extensions.Options.Options
尝试更换

   option = new Options<MyOptions>();


    option = Options.Create(new MyOptions());

关于c# - 如何创建IOptions <MyOption>的模拟实例?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48287699/

10-13 06:49