Moq/moq4
moq
The most popular and friendly mocking framework for .NET
var mock = new Mock<ILoveThisFramework>(); // WOW! No record/replay weirdness?! :)
mock.Setup(framework => framework.DownloadExists("2.0.0.0"))
.Returns(true)
.AtMostOnce(); // Hand mock.Object as a collaborator and exercise it,
// like calling methods on it...
ILoveThisFramework lovable = mock.Object;
bool download = lovable.DownloadExists("2.0.0.0"); // Verify that the given method was indeed called with the expected value
mock.Verify(framework => framework.DownloadExists("2.0.0.0"));
Moq also is the first and only framework so far to provide Linq to Mocks, so that the same behavior above can be achieved much more succintly:
ILoveThisFramework lovable = Mock.Of<ILoveThisFramework>(l =>
l.DownloadExists("2.0.0.0") == true); // Hand the instance as a collaborator and exercise it,
// like calling methods on it...
bool download = lovable.DownloadExists("2.0.0.0"); // Simply assert the returned state:
Assert.True(download); // If you really want to go beyond state testing and want to
// verify the mock interaction instead...
Mock.Get(lovable).Verify(framework => framework.DownloadExists("2.0.0.0"));
You can think of Linq to Mocks as "from the universe of mocks, give me one whose behavior matches this expression".
Checkout the Quickstart for more examples!
What?
Moq (pronounced "Mock-you" or just "Mock") is the only mocking library for .NET developed from scratch to take full advantage of .NET Linq expression trees and lambda expressions, which makes it the most productive, type-safe and refactoring-friendly mocking library available. And it supports mocking interfaces as well as classes. Its API is extremely simple and straightforward, and doesn't require any prior knowledge or experience with mocking concepts.
Why?
The library was created mainly for developers who aren't currently using any mocking library (or are displeased with the complexities of some other implementation), and who are typically manually writing their own mocks (with more or less "fanciness"). Most developers in this situation also happen to be quite pragmatic and adhere to state (or classic) TDD. It's the result of feeling that the barrier of entry from other mocking libraries is a bit high, and a simpler, more lightweight and elegant approach is possible. Moq achieves all this by taking full advantage of the elegant and compact C# and VB language features collectively known as LINQ (they are not just for queries, as the acronym implies).
Moq is designed to be a very practical, unobtrusive and straight-forward way to quickly setup dependencies for your tests. Its API design helps even novice users to fall in the "pit of success" and avoid most common misuses/abuses of mocking.
When it was conceived, it was the only mocking library that went against the generalized and somewhat unintuitive (especially for novices) Record/Replay approach from all other frameworks (and that might have been a good thing ;)).
Not using Record/Replay also means that it's straightforward to move common expectations to a fixture setup method and even override those expectations when needed in a specific unit test.
You can read more about the "why" and see some nice screenshots at kzu's blog.
Where?
See our Quickstart examples to get a feeling of the extremely simple API and install from nuget. Check out the API documentation at NuDoq.
Read about the announcement at kzu's blog. Get some background on the state of mock libraries from Scott Hanselman.
Who?
Moq was originally developed by Clarius, Manas and InSTEDD.
Moq uses Castle DynamicProxy internally as the interception mechanism to enable mocking. It's merged into Moq binaries, so you don't need to do anything other than referencing Moq.dll, though.
Features at a glance
Moq offers the following features:
- Strong-typed: no strings for expectations, no object-typed return values or constraints
- Unsurpassed VS intellisense integration: everything supports full VS intellisense, from setting expectations, to specifying method call arguments, return values, etc.
- No Record/Replay idioms to learn. Just construct your mock, set it up, use it and optionally verify calls to it (you may not verify mocks when they act as stubs only, or when you are doing more classic state-based testing by checking returned values from the object under test)
- VERY low learning curve as a consequence of the previous three points. For the most part, you don't even need to ever read the documentation.
- Granular control over mock behavior with a simple [http://www.clariusconsulting.net/labs/moq/html/90760C57.htm MockBehavior] enumeration (no need to learn what's the theoretical difference between a mock, a stub, a fake, a dynamic mock, etc.)
- Mock both interfaces and classes
- Override expectations: can set default expectations in a fixture setup, and override as needed on tests
- Pass constructor arguments for mocked classes
- Intercept and raise events on mocks
- Intuitive support for out/ref arguments
Need a commercial license or premium support?
You don't need a commercial license just to use Moq, even if it is for a commercial product, unless your company just doesn't use "open source software", in which case, a commercial license means they are buying the product like they would any other piece of software for the company.
If you still think you need a commercial license or just want premium support, contact us.
We appreciate deeply any feedback that you may have!
Moq/moq4的更多相关文章
- 【MVC 4】4.MVC 基本工具(Visual Studio 的单元测试、使用Moq)
作者:[美]Adam Freeman 来源:<精通ASP.NET MVC 4> 3.Visual Studio 的单元测试 有很多.NET单元测试包,其中很多是开源和免费的.本 ...
- Moq的使用
参考资料: 1. http://www.codeproject.com/Tips/729646/TDD-using-MOQ 2. https://github.com/Moq/moq4/wiki/Qu ...
- 使用 Moq 测试.NET Core 应用 - Why Moq?
什么是Mock 当对代码进行测试的时候, 我们经常需要用到一些模拟(mock)技术. 绿色的是需要被测试的类, 黄色是它的依赖项, 灰色的无关的类 在一个项目里, 我们经常需要把某一部分程序独立出来以 ...
- 使用 Moq 测试.NET Core 应用 -- 其它
第一篇文章, 关于Mock的概念介绍: https://www.cnblogs.com/cgzl/p/9294431.html 第二篇文章, 关于方法Mock的介绍: https://www.cnbl ...
- Moq 在.net Core 单元测试中的使用
Moq,主要用来伪造接口的实现类,实现方法,属性 moq The most popular and friendly mocking framework for .NET What? Moq (pro ...
- .NET 单元测试的利剑——模拟框架Moq(简述篇)
.NET 单元测试的利剑--模拟框架Moq 前言 这篇文章是翻译文,因为通过自己参与的项目,越发觉得单元测试的重要性,特别是当跟业务数据打交道的时候的,Moq就如雪中送炭,所以想学习这个框架,就从这篇 ...
- 学习笔记之Moq
dotnet/src/MoqSample at master · haotang923/dotnet · GitHub https://github.com/htanghtang/dotnet/tre ...
- MVC 基本工具(Visual Studio 的单元测试、使用Moq)
3.Visual Studio 的单元测试 有很多.NET单元测试包,其中很多是开源和免费的.本文打算使用 Visual Studio 附带的内建单元测试支持,但其他一些.NET单元测试包也是可用的. ...
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
常量,字段,构造方法 常量 1.什么是常量 常量是值从不变化的符号,在编译之前值就必须确定.编译后,常量值会保存到程序集元数据中.所以,常量必须是编译器识别的基元类型的常量,如:Boolean ...
随机推荐
- Bootstrap框架(组件)
按钮组 通过按钮组容器把一组按钮放在同一行里.通过与按钮插件联合使用,可以设置为单选框或多选框的样式和行为. 按钮组中的工具提示和弹出框需要特别的设置 当为 .btn-group 中的元素应用工具提示 ...
- springmvc项目,浏览器报404错误的问题
问题描述: 建立了web工程,配置pom.xml,web.xml,编写controller类,在spring-mvc-servlet.xml文件中指定开启注解和扫描的包位置<mvc:annota ...
- 【RL系列】Multi-Armed Bandit笔记补充(一)
在此之前,请先阅读上一篇文章:[RL系列]Multi-Armed Bandit笔记 本篇的主题就如标题所示,只是上一篇文章的补充,主要关注两道来自于Reinforcement Learning: An ...
- 转战Java~
记得16年5月份开始学的Java,当时就是为了学Hadoop才学的Java基础,之后Hadoop没学成,倒是学了Java Web的东西,当时就是玩玩,然后弄了个WeChat后台,就完事了.然后就又回到 ...
- js单行写一个评级组件
单行写一个评级组件:"★★★★★☆☆☆☆☆".slice(5 - rate, 10 - rate); -----------------------------------分隔符- ...
- 左值&右值
一.引子 我们所谓的左值.右值,正确的说法应该是左值表达式.右值表达式. 因为C++的表达式不是左值就是右值. 在C中,左值指的是既能够出现在等号左边也能出现在等号右边的表达式,右值指的则是只能出现在 ...
- <Effective C++>读书摘要--Designs and Declarations<三>
<Item 22> Declare data members private 1.使数据成员private,保持了语法的一致性,client不会为访问一个数据成员是否需要使用括号进行函数调 ...
- Winform 数据绑定
1.DataGridView数据绑定 namespace WindowsFormsApplication1 { public partial class Form1 : Form { private ...
- SQL SERVER技术内幕之5 表表达式
表表达式是一种命名的查询表达式,代表一个有效的关系表.可以像其他表一样,在数据处理语句中使用表表达式.SQL Server支持4种类型的表表达式:派生表(derived table).公用表表达式(C ...
- 在windows搭建react
1.安装必须的软件 1.Python 2 注意勾选 Add python.exe to Path,选项,这样就可以在安装完成后,不用手动去添加环境变量 安装完,打开cmd.exe,输入py ...