Moqzilla 1.1.0
Install-Package Moqzilla -Version 1.1.0
dotnet add package Moqzilla --version 1.1.0
<PackageReference Include="Moqzilla" Version="1.1.0" />
paket add Moqzilla --version 1.1.0
#r "nuget: Moqzilla, 1.1.0"
// 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
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 |
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
See Project URL for usage instructions.