Repo for managing Moq 4.x
C# Other
Latest commit 151bdc7 Feb 22, 2017 @kzu kzu committed on GitHub I screwed our semver, "fix" it
Permalink
Failed to load latest commit information.
Common @ 354e54d Finally added automated nuget package builds. Aug 21, 2013
Moq.VisualBasicTests Remove reference to nunit.framework from the VB test project Feb 21, 2017
Samples Re-normalize repository Dec 13, 2013
Source.NetCore Fix build errors by adding the new source file as a link Feb 21, 2017
Source Fix an issue where ISerializable is not available on .NET Core Feb 21, 2017
UnitTests.NetCore Upgrade Castle.Core dependency to 4.0.0 (#323) Feb 18, 2017
UnitTests.Visualizer Re-normalize repository Dec 13, 2013
UnitTests Merge branch 'master' into netcore Feb 20, 2017
Visualizer Re-normalize repository Dec 13, 2013
wrap/Moq Upgrade Castle.Core dependency to 4.0.0 (#323) Feb 18, 2017
.editorconfig Add .editorconfig May 26, 2016
.gitattributes Added CRLF settings to repo Jul 8, 2013
.gitignore Remove redundant build for the .NET Core project since it is already … May 23, 2016
.gitmodules Finally added automated nuget package builds. Aug 21, 2013
Before.Moq.sln.targets Migrate to NuGet v3, move to .NET v4.5, update build May 22, 2016
GitInfo.txt I screwed our semver, "fix" it Feb 22, 2017
License.txt License is BSD-3, not BSD-2 Sep 7, 2015
Moq.NetCore.sln Move the wrapped projects for Castle.Core and Moq out of the UnitTest… Dec 9, 2015
Moq.nuspec Upgrade Castle.Core dependency to 4.0.0 (#323) Feb 18, 2017
Moq.shfb Remove more Silverlight code. May 25, 2016
Moq.shfbproj Remove more Silverlight code. May 25, 2016
Moq.sln Merge branch 'master' into netcore Feb 20, 2017
Moq.snk Merged branch for dynamic types. Jan 3, 2008
NuGet.Restore.targets Migrate to NuGet v3, move to .NET v4.5, update build May 22, 2016
README.md Remove downloads badge Jun 9, 2016
ReleaseNotes.md Correct version number. May 26, 2016
appveyor.yml Bump version for public stable Feb 21, 2017
build.cmd Allow overriding default targets via .cmd May 26, 2016
build.proj Merge changes from upstream/master. May 26, 2016
global.json Upgrade Castle.Core dependency to 4.0.0 (#323) Feb 18, 2017
moq-bigger.png Updated to use nuget packages for Castle and xUnit dependencies, as w… Aug 21, 2013
moq-small.png cleaned up AsMockExtensions Aug 9, 2009
moq.png tentative logo Aug 24, 2008
packages.config Fix versioning issue with cached git info May 26, 2016

README.md

moq

The most popular and friendly mocking framework for .NET

Version Join the chat at https://gitter.im/Moq

  var mock = new Mock<ILoveThisFramework>();

  // WOW! No record/replay weirdness?! :)
  mock.Setup(framework => framework.DownloadExists("2.0.0.0"))
      .Returns(true);

  // 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 at most once
  mock.Verify(framework => framework.DownloadExists("2.0.0.0"), Times.AtMostOnce());

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

We appreciate deeply any feedback that you may have!

OhLoh

ClariusLabs