Skip to content
master
Switch branches/tags
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 

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

Explicitly supported frameworks

  • .NET Framework 4.6.1
  • .NET Standard 2.0

About

Simple automatic mocking using Moq

Resources

License

Releases

No releases published

Packages

No packages published

Languages