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 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!

时间: 2024-11-02 13:42:16

Moq/moq4的相关文章

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

 作者:[美]Adam Freeman      来源:<精通ASP.NET MVC 4> 3.Visual Studio 的单元测试 有很多.NET单元测试包,其中很多是开源和免费的.本文打算使用 Visual Studio 附带的内建单元测试支持,但其他一些.NET单元测试包也是可用的. 为了演示Visual Studio的单元测试支持,本例打算对示例项目添加一个 IDiscountHelper 接口的新实现. 在 Models 文件夹下新建类文件 MinimumDiscountHelpe

Moq的使用

参考资料: 1. http://www.codeproject.com/Tips/729646/TDD-using-MOQ 2. https://github.com/Moq/moq4/wiki/Quickstart 从https://github.com/Moq/moq4下载编译,且编译需要依赖Castle.Core这个包

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

3.Visual Studio 的单元测试 有很多.NET单元测试包,其中很多是开源和免费的.本文打算使用 Visual Studio 附带的内建单元测试支持,但其他一些.NET单元测试包也是可用的. 为了演示Visual Studio的单元测试支持,本例打算对示例项目添加一个 IDiscountHelper 接口的新实现. 在 Models 文件夹下新建类文件 MinimumDiscountHelper.cs : namespace EssentiaTools.Models { public

TDD:使用Moq进行Unit Test

什么时候使用Moq 对于下面的代码 public class ProductBusiness { public void Save() { try { var repository = new ProductRepository(); resository.Save(); } catch(AException ae) { ... } catch(BException be) { ... } } } 如果我们要对ProductBusiness.Save()方法进行UT,那么该怎么做才能让UT应该包

工具集

好用的工具集 转:http://www.yimingzhi.com/2015/03/lao-yi-de-kai-fa-gong-ju-he-lei-ku-ji-2014-ban Visual Studio 2013 扩展 Visual Studio 2013 Update 4:是目前微软发布的最新版开发工具升级包,高效而且强大.下面的扩展都是该版本的Visual Studio下的,老版本可以根据名字自行查找安装. CodeMaid: 可快速整理代码文件,清理不必要的代码和杂乱的格式.并在开发时实

C# 常用工具合集

Visual Studio 2013 扩展 Visual Studio 2013 Update 4:是目前微软发布的最新版开发工具升级包,高效而且强大.下面的扩展都是该版本的Visual Studio下的,老版本可以根据名字自行查找安装. CodeMaid: 可快速整理代码文件,清理不必要的代码和杂乱的格式.并在开发时实时提供代码复杂度的报告,以便帮助开发人员降低代码复杂度.提高代码质量. CssCop:可以帮助开发者检查和编写优秀的css代码,提高css对浏览器的兼容性.编码质量和渲染性能.

专业上的常用的工具和类库集 By 老衣

Visual Studio 2013 扩展 CodeMaid: 可快速整理代码文件,清理不必要的代码和杂乱的格式.并在开发时实时提供代码复杂度的报告,以便帮助开发人员降低代码复杂度.提高代码质量. CssCop:可以帮助开发者检查和编写优秀的css代码,提高css对浏览器的兼容性.编码质量和渲染性能. NuGet Package Manager for Visual Studio 2013: 一组用于自动执行从VS项目中安装.升级.配置和删除依赖包的过程的工具.本文档中的前后端技术选型中的绝大部

ASP.NET AJAX Control Toolkit

https://ajaxcontroltoolkit.codeplex.com/ 警告 7 未能找到引用的组件“Antlr3.Runtime”. 警告 6 未能找到引用的组件“HtmlAgilityPack”. 警告 10 未能找到引用的组件“HtmlAgilityPack”. 警告 1 未能找到引用的组件“Moq”. 警告 3 未能找到引用的组件“nunit.core.interfaces”. 警告 2 未能找到引用的组件“nunit.core”. 警告 4 未能找到引用的组件“nunit.f

老衣的开发工具和类库集之2014版

转载自:http://www.yimingzhi.com/2015/03/lao-yi-de-kai-fa-gong-ju-he-lei-ku-ji-2014-ban 今天是2015年的元宵节了,14年承诺朋友们公开自己常用的工具和类库集,一直没能及时兑现.今天忙里偷个闲,整理了一下清单,在此公布出来,希望能够对大家在.NET.App.Web等开发方面有所帮助.也当是给.NET开发者一个元宵礼物吧,哈哈.不废话直接上菜啦~ [注:最下方有补充更新] Visual Studio 2013 扩展Vi