What is use of Moq?

China☆狼群 提交于 2020-02-17 06:11:08

问题


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?


回答1:


Moq is a mocking framework for C#/.NET. It is used in unit testing to isolate your class under test from its dependencies and ensure that the proper methods on the dependent objects are being called. For more information on mocking you may want to look at the Wikipedia article on Mock Objects.

Other mocking frameworks (for .NET) include JustMock, TypeMock, RhinoMocks, nMock, .etc.




回答2:


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.




回答3:


Moq is a mocking engine for doing .Net TDD.



来源:https://stackoverflow.com/questions/678878/what-is-use-of-moq

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