繁体   English   中英

模拟单元测试方法

[英]Mocking a method for unit testing

我想模拟以下采用参数的方法,以便使用假货和垫片进行单元测试。 我不知道它对我来说是新的。 任何想法都会有所帮助。

public string Renotify(int[] userIds)
{
     var notify = new NotificationPublisher();
     var message = "A request has been awaiting for your approval. Please check the application for details to approve the request ";
     var subject = "Logos Approval Notification";

     if (userIds.Length < 1)
         return "Please select users to notify";

     List<NotificationUser> userList = userIds.Select(t => new NotificationUser { userId = t }).ToList();
     notify.SendNotification(userList, message, subject);
     return "Success - Approvers Renotified";

}

您不能模拟一个方法,也就是说,您不能模拟要测试的东西所使用的接口。

相反,您可以,但是我不能确定这就是您真正想要的。

例如

public class Foo
{
    private readonly IBar _bar;

    public Foo(IBar bar)
    {
        _bar = bar;
    }

    public void MyMethodToTest()
    {
        // Some stuff
        _bar.SomethingToBeMocked();
        //some more stuff
    }
}

IBar接口:

public IBar
{
    void SomethingToBeMocked();
}

您可以模拟接口IBar ,然后在IBar.SomethingToBeMocked()上设置预期的调用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM