.NET Core Unit Testing - Mock IOptions

前端 未结 8 903
难免孤独
难免孤独 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条回答
  •  一个人的身影
    2020-12-12 18:15

    Here's another easy way that doesn't need Mock, but instead uses the OptionsWrapper:

    var myAppSettingsOptions = new MyAppSettingsOptions();
    appSettingsOptions.MyObjects = new MyObject[]{new MyObject(){MyProp1 = "one", MyProp2 = "two", }};
    var optionsWrapper = new OptionsWrapper(myAppSettingsOptions );
    var myClassToTest = new MyClassToTest(optionsWrapper);
    

提交回复
热议问题