Moqzilla 1.1.0

.NET Standard 2.0 .NET Framework 4.6.1
Install-Package Moqzilla -Version 1.1.0
dotnet add package Moqzilla --version 1.1.0
<PackageReference Include="Moqzilla" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Moqzilla --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Moqzilla, 1.1.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Moqzilla as a Cake Addin
#addin nuget:?package=Moqzilla&version=1.1.0

// Install Moqzilla as a Cake Tool
#tool nuget:?package=Moqzilla&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Moqzilla

Build status

Simple automatic mocking using Moq.

Example Setup

Assume we have an interface like this:

public interface IDependency
{
    int GetTheNumber();
}

And a class that consumes it like this:

public class MyClass
{
    private readonly IDependency _dependency;

    public MyClass(IDependency dependency)
    {
        _dependency = dependency;
    }
    
    public int GetDoubleTheNumber()
    {
        return _dependency.GetTheNumber() * 2;
    }
}

General Usage

Moqzilla works with Moq in an NUnit test like so:

[Test]
public void MyTest()
{
    // Arrange.
    var mocker = new Mocker();
    var myObject = mocker.Create<MyClass>();
    
    var mockedDependency = mocker.Mock<IDependency>();
    mockedDependency.Setup(m => m.GetTheNumber()).Returns(4);
    
    // Act.
    var observedValue = myObject.GetDoubleTheNumber();

    // Assert.
    Assert.AreEqual(8, observedValue);
}

It will also work if you construct your mock before creating the object.

Mock Blocks

To keep mock setup more isolated, configuration blocks can be used like so:

[Test]
public void MyTest()
{
    // Arrange.
    var mocker = new Mocker();
    var myObject = mocker.Create<MyClass>();
    
    mocker.Mock<IDependency>(mock => {
        mock.Setup(m => m.GetTheNumber()).Returns(4);
    });
    
    // Act.
    var observedValue = myObject.GetDoubleTheNumber();

    // Assert.
    Assert.AreEqual(8, observedValue);
}

Activation

Mocks can be configured as objects are created. Activators for a dependency are run each time Mocker.Create<T> is called, if they are in the constructor.

[Test]
public void MyTest()
{
    // Arrange.
    var mocker = new Mocker();
    mocker.Activate<IDependency>(mock => mock.Setup(m => m.GetTheNumber()).Returns(4));

    var myObject = mocker.Create<MyClass>();

    // Act.
    var observedValue = myObject.GetDoubleTheNumber();

    // Assert.
    Assert.AreEqual(8, observedValue);
}

Injecting Implementations

Sometimes, for the sake of brevity, concrete implementations of an interface might be desired. In this case, Mocker.Implement<T> can be used.

Suppose we have a dependency that looks kind of like this:

public class Dependency : IDependency
{
    public int Value => 4;
}

public interface IDependency
{
    int Value { get; }
}

And something that consumes the dependency like so...

public class Consumer
{
   private readonly IDependency _dependency;

   public Consumer(IDependency dependency)
   {
       _dependency = dependency;
   }
   
   public GetValue()
   {
       return _dependency.Value;
   }
}

We can then use the concrete implementation during testing.

[Test]
public void MyTest()
{
    // Arrange.
    var mocker = new Mocker();
    var myDependency = new Dependency();
    mocker.Implement<IDependency>(myDependency);

    var myObject = mocker.Create<MyClass>();

    // Act.
    var observedValue = myObject.GetValue();
    
    // Assert.
    Assert.AreEqual(4, observedValue);
}

Prerequisites

  • Moq
    • 4.2+ for .NET Framework
    • 4.7+ for .NET Standard

Explicitly supported frameworks

  • .NET Framework 4.0
  • .NET Framework 4.5
  • .NET Standard 1.3
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.6.1

    • Moq (>= 4.10.0)
  • .NETStandard 2.0

    • Moq (>= 4.10.0)

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 777 10/18/2018
1.0.4 787 10/9/2017
1.0.3 794 6/27/2017
1.0.2 789 6/27/2017
1.0.1 913 6/20/2017
1.0.0 782 6/15/2017
0.0.2 985 6/14/2017
0.0.1 973 6/14/2017

See Project URL for usage instructions.