Moq testing void method

戏子无情 提交于 2019-12-04 02:23:37

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.

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.

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?

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

Better to provide more context, but typically it used like this:

var mockAdd = new Mock<IAdd>();
mockAdd.Setup(x => x.Add(1, 2)).Verifiable();

//do something here what is using mockAdd.Add

mockAdd.VerifyAll();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!