Populate IConfiguration for unit tests

前端 未结 5 792
别那么骄傲
别那么骄傲 2021-01-17 07:37

.NET Core configuration allows so many options to add values (environment variables, json files, command line args).

I just can\'t figure out and find an answer how

5条回答
  •  日久生厌
    2021-01-17 08:12

    You can use the following technique to mock IConfiguration.GetValue(key) extension method.

    var configuration = new Mock();
    var configSection = new Mock();
    
    configSection.Setup(x => x.Value).Returns("fake value");
    configuration.Setup(x => x.GetSection("MySection")).Returns(configSection.Object);
    //OR
    configuration.Setup(x => x.GetSection("MySection:Value")).Returns(configSection.Object);
    

提交回复
热议问题