【问题标题】:Mocking a method for unit testing模拟单元测试的方法
【发布时间】:2015-10-21 21:29:59
【问题描述】:

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

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

}

【问题讨论】:

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


    【解决方案1】:

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

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

    例如

    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() 上设置预期调用

    【讨论】:

      猜你喜欢
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多