如何使用Moq模拟IConfiguration?

5

我该如何在单元测试中 mock 类似这样的代码。我正在使用 asp.net core 5 中的 xUnit 和 Moq。我对 xUnit 和 Moq 不熟悉。

var url = configuration.GetSection("AppSettings").GetSection("SmsApi").Value;

配置对象已注入到构造函数中。
这是我当前在单元测试类中的代码。
public class UtilityTests
{
    private readonly Utility sut;

    public UtilityTests()
    {
        var mockConfig = new Mock<IConfiguration>();
        var mockConfigSection = new Mock<IConfigurationSection>();
        //mockConfigSection.Setup(x => x.Path).Returns("AppSettings");
        mockConfigSection.Setup(x => x.Key).Returns("SmsApi");
        mockConfigSection.Setup(x => x.Value).Returns("http://example.com");
        
        mockConfig.Setup(x => x.GetSection("AppSettings")).Returns(mockConfigSection.Object);
        
        sut = new Utility(mockConfig.Object);
    }

    [Fact]
    public void SendSmsShdReturnTrue()
    {
        var fixture = new Fixture();
        
        var result = sut.SendSms(fixture.Create<string>(), fixture.Create<string>());
        result.Should().BeTrue();
    }
}

1
是 appSettings.json 还是 web.config?提供一个示例文件会很有帮助。 - dotnetstep
这是.NET Core,所以应用程序的设置在appsettings.json文件中。 - plasteezy
@plasteezy,那些建议的解决方案有没有对你起作用? - Peter Csala
2个回答

11

事实上,IConfiguration 不应该被模拟。相反,它应该被 构建

通过字典

数据

var configForSmsApi = new Dictionary<string, string>
{
    {"AppSettings:SmsApi", "http://example.com"},
};

使用方法

var configuration = new ConfigurationBuilder()
    .AddInMemoryCollection(configForSmsApi)
    .Build();

通过JSON文件

数据

{
  "AppSettings": {
    "SmsApi": "http://example.com"
  }
}

使用方法

var configuration = new ConfigurationBuilder()
    .AddJsonFile("smsapi.json", optional: false)
    .Build();

为什么不应该模拟 IConfiguration - Yíu
1
@Yíu 可以进行模拟,但是通过字典构建会更容易。否则,您必须模拟多个具有不同参数的“GetSection”(请参见OP的示例)。 - Peter Csala

3

用一种替代方法引入一个类来表示配置文件的部分,然后使用IOptions接口将其注入到构造函数中。

这样,您的测试就可以简单地进行配置,无需模拟,只需创建一个实例并将其传递给构造函数即可。

像下面这样:

class SmsApiSettings
{
    public string Url { get; set; }
}

注册启动过程中

services.Configure<SmsApiSettings>(Configuration.GetSection("SmsApi"));

构造函数

public class ClassUnderTest
{
    private readonly SmsApiSettings _smsApiSettings;

    public ClassUnderTest(IOptions<> smsOptions)
    {
        _smsApiSettings = smsOptions.Value;
    }
}

测试

var settings = new SmsApiSettings { Url = "http://dummy.com" };
var options = Options.Create(settings);

var sut = new ClassUnderTest(options);

享受快乐生活,无需嘲讽 ;)


我喜欢这种方法,这是一种更优雅的处理方式。谢谢 @Fabio - plasteezy
我觉得多提供一些背景信息会更好。(关于代码片段的周边情况。) - Scott Fraley

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接