How to initialize IOption for unit testing a .NET core MVC service?

前端 未结 1 1043
借酒劲吻你
借酒劲吻你 2021-02-03 19:18

I have a .NET core MVC rest service. I have a controller I wish to test. This controller has a constructor argument of IOptions where AppSettings is my class for config setting

相关标签:
1条回答
  • 2021-02-03 20:10

    I discovered the answer shortly after posting the question.

    use Helper class Microsoft.Extensions.Options.Options

    Creates a wrapper around an instance of TOptions to return itself as IOptions

    AppSettings appSettings = new AppSettings() { ConnectionString = "..." };
    IOptions<AppSettings> options = Options.Create(appSettings);
    MyController controller = new MyController(options);
    
    0 讨论(0)
提交回复
热议问题