Moqzilla 1.1.0

Automatic test subject mocking using Moq.

Install-Package Moqzilla -Version 1.1.0
dotnet add package Moqzilla --version 1.1.0
paket add 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&lt;T&gt; 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&lt;T&gt; 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

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&lt;T&gt; 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&lt;T&gt; 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

Release Notes

See Project URL for usage instructions.

  • .NETFramework 4.6.1

    • Moq (>= 4.10.0)
  • .NETStandard 2.0

    • Moq (>= 4.10.0)

Version History

Version Downloads Last updated
1.1.0 52 10/18/2018
1.0.4 155 10/9/2017
1.0.3 155 6/27/2017
1.0.2 135 6/27/2017
1.0.1 182 6/20/2017
1.0.0 140 6/15/2017
0.0.2 215 6/14/2017
0.0.1 212 6/14/2017