Moq - The simplest mocking library for .NET and Silverlight
2013-03-21 15:09
513 查看
Update: Development of this project has moved to GitHub. New releases and bug fixes will be published there.
Checkout the QuickStart for more examples!
Moq (pronounced "Mock-you" or just "Mock") is the only mocking library for .NET developed from scratch to take full advantage of .NET 3.5 (i.e. Linq expression trees) and C# 3.0 features (i.e. lambda expressions) that make 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.
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# 3.0 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.
Currently, it's the only mocking library that goes against the generalized and somewhat unintuitive (especially for novices) Record/Replay approach from all other frameworks (and this
might be 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.
See our QuickStart examples to get a feeling of the extremely simple API and download
the latest binaries, source code and help file.
Read about the announcement at kzu's blog. Get some background on the
state of mock libraries from Scott Hanselman.
Browse the API documentation to get a feeling of how extremely simple it is to use.
Moq was jointly developed by Clarius, Manas and InSTEDD.
You can read more about it at InSTEDD Moq page.
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.
Moq team uses VisualSVN to easily work with the SVN repository. It makes development a breeze!
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 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
Check the ChangeLog to learn about features introduced with each version.
Checkout some adoption stats, and hear what Moq users have to say in ILoveMoq.
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.
Checkout our HowToContribute page and get the Moq Contrib project.
We also appreciate deeply any feedback that you may have!
![](http://www.ohloh.net/p/moq/widgets/project_thin_badge.gif)
![](http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=clarius&DownloadId=17830&Build=14806&boo.png)
Referenced from: https://code.google.com/p/moq/
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"));
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 3.5 (i.e. Linq expression trees) and C# 3.0 features (i.e. lambda expressions) that make it the most productive, type-safeand 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 manuallywriting 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# 3.0 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.
Currently, it's the only mocking library that goes against the generalized and somewhat unintuitive (especially for novices) Record/Replay approach from all other frameworks (and this
might be 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 downloadthe latest binaries, source code and help file.
Read about the announcement at kzu's blog. Get some background on the
state of mock libraries from Scott Hanselman.
Browse the API documentation to get a feeling of how extremely simple it is to use.
Who?
Moq was jointly developed by Clarius, Manas and InSTEDD.You can read more about it at InSTEDD Moq page.
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.
Moq team uses VisualSVN to easily work with the SVN repository. It makes development a breeze!
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 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
Check the ChangeLog to learn about features introduced with each version.
Who's using this?
Checkout some adoption stats, and hear what Moq users have to say in ILoveMoq.
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 ofsoftware for the company.
If you still think you need a commercial license or just want premium support, contact us.
Want to join and contribute?
Checkout our HowToContribute page and get the Moq Contrib project.We also appreciate deeply any feedback that you may have!
![](http://www.ohloh.net/p/moq/widgets/project_thin_badge.gif)
![](http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=clarius&DownloadId=17830&Build=14806&boo.png)
Referenced from: https://code.google.com/p/moq/
相关文章推荐
- ASP.NET Podcast Show #11 - Intro to Ajax and the Ajax Library for .NET
- Comparing the Microsoft .NET Framework 1.1 Data Provider for Oracle and the Oracle Data Provider for .NET
- The Windows SDK team is proud to announce that the RTM release of the Microsoft Windows SDK for Windows Server 2008 and .NET Fra
- .NET Security Library for C# and VB.NET - Org.Mentalis.SecurityServices.dll
- Tips and Tricks for the Visual Studio .NET IDE
- Setup 64-bit Windows Server for Running ASP.NET 1.1 and ASP.NET 2.0 at the Same Time
- 概述CSLA.NET 3.6 (Overview of CSLA .NET 3.6 for Windows and Silverlight)
- Debugging Applications for Microsoft .NET and Microsoft Windows: Notes-The Debugging Process
- 安装Windows SDK for Windows Server 2008 and .NET Framework 3.5之后,WPF和Silverlight没有了智能提示
- C++中使用array报错 requires compiler and library surpport for the ISO c++ 2011 standard
- 概述CSLA.NET 3.6 (Overview of CSLA .NET 3.6 for Windows and Silverlight)
- The Zip, GZip, BZip2 and Tar Implementation For .NET
- Tips and Tricks for the Visual Studio .NET IDE
- NWebsec 1.0.3 The Security library for ASP.NET
- Creating a Compelling UI for Windows Mobile and the Microsoft .NET Compact Framework
- Setup 64-bit Windows Server for Running ASP.NET 1.1 and ASP.NET 2.0 at the Same Time
- Ajax.NET - A free library for the Microsoft .NET Framework
- Creating a Compelling UI for Windows Mobile and the Microsoft .NET Compact Framework
- The USB library for .NET
- 概述CSLA.NET 3.6 (Overview of CSLA .NET 3.6 for Windows and Silverlight)