Moqzilla 1.1.0

dotnet add package Moqzilla --version 1.1.0
NuGet\Install-Package Moqzilla -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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
#r "nuget: Moqzilla, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. 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

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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
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 1,080 10/18/2018
1.0.4 995 10/9/2017
1.0.3 1,017 6/27/2017
1.0.2 1,006 6/27/2017
1.0.1 1,160 6/20/2017
1.0.0 992 6/15/2017
0.0.2 1,273 6/14/2017
0.0.1 1,241 6/14/2017

See Project URL for usage instructions.