模拟单元测试的方法

时间:2015-07-30 13:31:08

标签: c# unit-testing mocking microsoft-fakes shim

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

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";

}

1 个答案:

答案 0 :(得分:0)

你不能模拟一个方法,说,没有意义,你模拟你要测试的东西使用的接口。

  

相反,你可以,但我不相信这是你真正想要的。

e.g。

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()

上设置了预期的调用