Moq testing void method

前端 未结 2 2045
别跟我提以往
别跟我提以往 2021-02-19 18:51

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);
 }
         


        
2条回答
  •  梦谈多话
    2021-02-19 19: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.

提交回复
热议问题