C# 起订量有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/678878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is use of Moq?
提问by leen3o
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?
我一直在 DotNetKicks 等网站上看到这一点......但无法确切地找出它是什么(英文)或它有什么作用?你能解释一下它是什么,或者我为什么要使用它?
采纳答案by tvanfosson
Moqis 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.
Moq是 C#/.NET 的模拟框架。它在单元测试中用于将被测类与其依赖项隔离,并确保调用依赖对象上的正确方法。有关模拟的更多信息,您可能需要查看关于模拟对象的维基百科文章。
Other mocking frameworks (for .NET) include JustMock, TypeMock, RhinoMocks, nMock, .etc.
其他模拟框架(用于 .NET)包括JustMock、TypeMock、 RhinoMocks、nMock等。
回答by Matt Grande
Moq is a mocking engine for doing .Net TDD.
Moq 是用于执行 .Net TDD 的模拟引擎。
回答by Rajesh Mishra
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:
简单来说,Moq 是一个库,当您将其包含在您的项目中时,您就可以轻松地进行单元测试。为什么?因为一个函数可能会调用另一个函数,然后再调用另一个函数,依此类推。但实际上需要的只是第一次调用的返回值以继续下一行。Moq 有助于忽略该方法的实际调用,而是返回该函数返回的内容。并在执行完所有代码行后验证,您想要的就是您得到的。太多的英语,所以这里是一个例子:
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:
现在,这里first
需要 for secondcall()
,但您实际上不能help.firstcall()
像在其他层那样调用它。这样 Mocking 就完成了,假装那个方法被调用了:
[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
.
在这里,想想,SetUP
作为伪造的方法调用,我们只是对Returns
.