模拟具有可选参数的方法

时间:2015-12-24 09:22:54

标签: c# .net visual-studio unit-testing moq

我有一个方法public void SendMail(string email, string subject, string body, List<Attachment> attachements = null),其中最后一个参数可以是nullList<Attachment>

我这样嘲笑:

        var mailMock = new Mock<IMailService>();
        mailMock.Setup(m => m.SendMail(It.IsNotNull<string>(), It.IsNotNull<string>(), It.IsNotNull<string>(), It.IsAny<List<Attachment>>())).Verifiable();
        mailMock.Verify();

但它不允许附件参数中的null值。如何同时传递nullList<Attachment>

更新:实际上,它正在运行,只是我在实际调用测试方法之前过早地调用了mailMock.Verify();

1 个答案:

答案 0 :(得分:-2)

编辑:不是一个好的答案,不是回答问题,而是一般的错误。

您不需要“= null”部分。每个可空对象(包含List)都可以为null。所以像这样的方法无论如何都会允许空值。

相关问题