.NET Core Unit Testing - Mock IOptions

前端 未结 8 864
难免孤独
难免孤独 2020-12-12 17:55

I feel like I\'m missing something really obvious here. I have classes that require injecting of options using the .NET Core IOptions pattern(?). When I unit te

8条回答
  •  萌比男神i
    2020-12-12 18:32

    You need to manually create and populate an IOptions object. You can do so via the Microsoft.Extensions.Options.Options helper class. For example:

    IOptions someOptions = Options.Create(new SampleOptions());
    

    You can simplify that a bit to:

    var someOptions = Options.Create(new SampleOptions());
    

    Obviously this isn't very useful as is. You'll need to actually create and populate a SampleOptions object and pass that into the Create method.

提交回复
热议问题