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 ClariusManas 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的更多相关文章

  1. 【MVC 4】4.MVC 基本工具(Visual Studio 的单元测试、使用Moq)

     作者:[美]Adam Freeman      来源:<精通ASP.NET MVC 4> 3.Visual Studio 的单元测试 有很多.NET单元测试包,其中很多是开源和免费的.本 ...

  2. Moq的使用

    参考资料: 1. http://www.codeproject.com/Tips/729646/TDD-using-MOQ 2. https://github.com/Moq/moq4/wiki/Qu ...

  3. 使用 Moq 测试.NET Core 应用 - Why Moq?

    什么是Mock 当对代码进行测试的时候, 我们经常需要用到一些模拟(mock)技术. 绿色的是需要被测试的类, 黄色是它的依赖项, 灰色的无关的类 在一个项目里, 我们经常需要把某一部分程序独立出来以 ...

  4. 使用 Moq 测试.NET Core 应用 -- 其它

    第一篇文章, 关于Mock的概念介绍: https://www.cnblogs.com/cgzl/p/9294431.html 第二篇文章, 关于方法Mock的介绍: https://www.cnbl ...

  5. Moq 在.net Core 单元测试中的使用

    Moq,主要用来伪造接口的实现类,实现方法,属性 moq The most popular and friendly mocking framework for .NET What? Moq (pro ...

  6. .NET 单元测试的利剑——模拟框架Moq(简述篇)

    .NET 单元测试的利剑--模拟框架Moq 前言 这篇文章是翻译文,因为通过自己参与的项目,越发觉得单元测试的重要性,特别是当跟业务数据打交道的时候的,Moq就如雪中送炭,所以想学习这个框架,就从这篇 ...

  7. 学习笔记之Moq

    dotnet/src/MoqSample at master · haotang923/dotnet · GitHub https://github.com/htanghtang/dotnet/tre ...

  8. MVC 基本工具(Visual Studio 的单元测试、使用Moq)

    3.Visual Studio 的单元测试 有很多.NET单元测试包,其中很多是开源和免费的.本文打算使用 Visual Studio 附带的内建单元测试支持,但其他一些.NET单元测试包也是可用的. ...

  9. 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq

    常量,字段,构造方法   常量 1.什么是常量 ​ 常量是值从不变化的符号,在编译之前值就必须确定.编译后,常量值会保存到程序集元数据中.所以,常量必须是编译器识别的基元类型的常量,如:Boolean ...

随机推荐

  1. Trie 树——搜索关键词提示

    当你在搜索引擎中输入想要搜索的一部分内容时,搜索引擎就会自动弹出下拉框,里面是各种关键词提示,这个功能是怎么实现的呢?其实底层最基本的就是 Trie 树这种数据结构. 1. 什么是 "Tri ...

  2. defineporperty 的使用 设置对象的只读或只写属性

    <!DOCTYPE html> <html lang="en"> <head> <title>Document</title& ...

  3. 第十六次ScrumMeeting会议

    第十六次Scrum Meeting 时间:2017/12/6 地点:线上+SPR咖啡馆 人员:蔡帜 王子铭 游心 解小锐 王辰昱 李金奇 杨森 陈鑫 照片: 目前工作进展 名字 今日 明天的工作 遇到 ...

  4. jspSmartUpload上传下载使用例子

    --------------------------------------------------------------------- ServletUpload.java 上传 package ...

  5. ACM 第十八天

    数学基础(卷积,FFT,FWT,FMT,鸽巢原理,群论,哈里亚余数,哈里亚计数定理,组合数学,LVG定理,期望DP,期望点贡献问题) 练习题: A - Necklace of Beads Beads ...

  6. OSG学习:多重纹理映射

    #include<osgViewer\Viewer> #include<osg\Node> #include<osg\Geode> #include<osg\ ...

  7. ipython matplotlib

    matplotlib实际上是一套面向对象的绘图库,它所绘制的图表中的每个绘图元素,例如线条Line2D.文字Text.刻度等在内存中都有一个对象与之对应.为了方便快速绘图matplotlib通过pyp ...

  8. Windows7系统目录迁移:Users,Progr…

    微软设计了比如:我的文档.我的OOXX,之类的东西,在WIN7下面更连游戏.下载等等目录都设计好了,我也很乖巧的把各种文件都分门别类的放进去了. 同时也很厉害的设计在了“%HOMEDRIVE%”里面, ...

  9. RT-thread内核对象标志flag总结

    一.内核标志flag 在内核对象控制块中有一个标志成员flag(rt_uint8_t flag; ),这个标志在不同有内核对象中具有不同的含义.rt-thread的内核对象有定时器.线程.信号量.互斥 ...

  10. javaBean默认接受request发送过来的数据,根据键自动设置属性

    javaBean默认接受request发送过来的数据,根据键自动设置属性