What is use of Moq?

后端 未结 3 1634
栀梦
栀梦 2021-01-31 09:47

I keep seeing this referred to on DotNetKicks etc... Yet cannot find out exactly what it is (In English) or what it does? Could you explain what it is, or why I would use it?

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-31 10:35

    In simple English, Moq is a library which when you include in your project give you power to do Unit Testing in easy manner. Why? Because one function may call another, then another and so on. But in real what is needed, just the return value from first call to proceed to next line. Moq helps to ignore actual call of that method and instead you return what that function was returning. and verify after all lines of code has executed, what you desired is what you get or not. Too Much English, so here is an example:

    String Somethod()
    {
      IHelper help = new IHelper();
      String first = help.firstcall();
      String second= secondcall(first);
      return second;
    }
    

    Now, here first is needed to for secondcall(), but you can not actually call help.firstcall() as it in some other layer. So Mocking is done, faking that method was called:

    [TestMethod]
    public void SomeMethod_TestSecond
    {
      mockedIHelper.Setup(x=>x.firstcall()).Returns("Whatever i want");  
    }
    

    Here, think, SetUP as faking method call, we are just interested in Returns.

提交回复
热议问题