最小起订量测试无效方法 [英] Moq testing void method

查看:87
本文介绍了最小起订量测试无效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Moq测试的新手,很难做一个简单的断言. 我正在使用界面

Hi I am new to Moq testing and having hard time to do a simple assertion. I am using an interface

 public interface IAdd
 {
     void add(int a, int b);
 }

IAdd界面的最小起订量为:

Moq for the IAdd interface is:

  Mock<IAdd> mockadd = new Mock<IAdd>();
  mockadd.Setup(x => x.add(It.IsAny<int>(), It.IsAny<int>()).callback((int a, int b) => { a+b;});
  IAdd testing = mockadd.Object;

由于add方法无效,因此不会向Assert使用任何值.如何声明此设置?

Since the add method is void, it doesn't return any value to Assert with. How can I assert this setup?

推荐答案

为什么要使用模拟?它用于验证SUT(被测系统)与其依赖关系是否正确交互(应进行模拟).正确的交互意味着使用正确的参数调用正确的依赖项成员.

Why mocking is used? It used for verifying that SUT (system under test) interacts correctly with its dependencies (which should be mocked). Correct interaction means calling correct dependency members with correct parameters.

您应该永远不要对模拟返回的值进行断言.这是与生产代码无关的伪值.您应该断言的唯一值是SUT返回的值. SUT是您唯一应该为其写断言的东西.

You should never assert on value returned by mock. That is dummy value which has no relation to production code. The only value you should assert on is a value returned by SUT. SUT is the only thing you should write assertions for.

您还应该从不测试界面.因为没有要测试的东西.接口只是一个API描述.它没有实现.那么,停下来思考一下您在这里测试什么代码?这是在您的应用程序中执行的真实代码吗?

Also you should never test interfaces. Because there is nothing to test. Interface is just a API description. It has no implementation. So, stop and think about what code are you testing here? Is this is a real code, which executed in your application?

因此,您应该仅对使用IAdd接口的测试对象进行模拟IAdd接口.

So, you should mock IAdd interface only for testing object which uses IAdd interface.

这篇关于最小起订量测试无效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆