Skip to main content

Mock test parameters

Declare the class under test and its dependencies as parameters of your test method and add [AutoMockArguments]. Mockolate fills the parameters, and the class under test uses the same mocks that your test receives, so you can set them up and verify them directly:

public class VendingMachine(IChocolateDispenser dispenser)
{
public string Buy(string type, int amount)
=> dispenser.Dispense(type, amount) ? "Enjoy!" : "Sold out";
}

public class VendingMachineTests
{
[Test]
[AutoMockArguments]
public async Task Buy_WhenChocolateIsAvailable_ShouldDispenseIt(VendingMachine sut, IChocolateDispenser dispenser)
{
dispenser.Mock.Setup.Dispense("Dark", It.IsAny<int>()).Returns(true);

string result = sut.Buy("Dark", 4);

await That(result).IsEqualTo("Enjoy!");
await That(dispenser.Mock.Verify.Dispense("Dark", 4)).Once();
}
}

Mockolate generates the attribute for the test framework of your project. The sections below show how to use it with TUnit and xUnit.

What each parameter gets​

  • An interface, delegate or abstract class gets a mock.
  • A class gets a mock that calls the real class. It behaves like the real class until you set up one of its virtual members, e.g. sut.Mock.Setup.GetPrice().Returns(1m) for a virtual GetPrice method.
  • A sealed class or a record gets a real instance.
  • Any other type, such as string or int, gets a default value.

The order of the parameters does not matter. A class receives the other parameters of the test method as its constructor arguments. A constructor parameter without a matching test parameter gets a new mock or a default value, which you can reach with sut.Mock.ConstructorArgument<IChocolateDispenser>().

A test parameter can fill several constructor parameters of its type. When several test parameters have the type of a constructor parameter, give one of them the name of the constructor parameter. A test parameter of a value type or string is only passed to a constructor parameter with its name.

Pass values​

The attribute takes values for the first parameters of the test method. Each attribute is one test case:

[Test]
[AutoMockArguments("Dark", 4)]
[AutoMockArguments("White", 2)]
public async Task Buy_ShouldDispenseTheRequestedChocolate(string type, int amount, VendingMachine sut,
IChocolateDispenser dispenser)
{
sut.Buy(type, amount);

await That(dispenser.Mock.Verify.Dispense(type, amount)).Once();
}

The values are only passed to the test method. A class under test does not receive them as constructor arguments.

Keep a mock out of the class under test​

Mark a parameter with [Transient] to get a mock that the class under test does not receive:

[Test]
[AutoMockArguments]
public async Task Buy_ShouldNotUseAnotherDispenser(VendingMachine sut, [Transient] IChocolateDispenser other)
{
sut.Buy("Dark", 4);

await That(other.Mock.Verify.Dispense(It.IsAny<string>(), It.IsAny<int>())).Never();
}

Configure the mocks​

Assign AutoMockArgumentsAttribute.DefaultBehavior to choose the behavior of the mocks and the default values, for example with a default value generator backed by a library like AutoFixture. Assign it in a module initializer, so that it is set before the first test case is created:

using System.Runtime.CompilerServices;

internal static class MockolateConfiguration
{
[ModuleInitializer]
internal static void Configure()
=> AutoMockArgumentsAttribute.DefaultBehavior =
() => MockBehavior.Default.WithDefaultValueGenerator(new AutoFixtureGenerator());
}

TUnit​

With TUnit 1.0 or later, the attribute is in the Mockolate.Integrations.TUnit namespace. Use it on a [Test], like TUnit's [Arguments]:

using Mockolate.Integrations.TUnit;

[Test]
[AutoMockArguments("Dark", 4)]
public async Task Buy_ShouldDispenseTheRequestedChocolate(string type, int amount, VendingMachine sut,
IChocolateDispenser dispenser)

xUnit​

With xUnit v3 or v2, the attribute is in the Mockolate.Integrations.Xunit namespace. Use it on a [Theory], like xUnit's [InlineData]:

using Mockolate.Integrations.Xunit;

[Theory]
[AutoMockArguments("Dark", 4)]
public async Task Buy_ShouldDispenseTheRequestedChocolate(string type, int amount, VendingMachine sut,
IChocolateDispenser dispenser)

A project that references both v2 and v3 gets the attribute for v3. With v3, the attribute takes the settings of xUnit's data attributes, such as Skip, Explicit, Timeout and Traits. A theory with mocks shows up as one test in the test explorer, with a result for each attribute. xUnit v2 may already create the arguments of the first attribute when it discovers the tests.

xUnit describes the arguments in the name of a test case and disposes them after the test. To describe a mock of a collection interface, such as ISet<T>, it may enumerate the mock, and a mock of a disposable interface gets a call to Dispose. Both calls count as interactions of the mock. On a strict mock, set up Dispose, or xUnit reports a cleanup error.

Details and limitations
  • Set <MockolateTestFrameworkIntegration>false</MockolateTestFrameworkIntegration> in the project file to skip the attribute.
  • A test parameter matches a constructor parameter of exactly its type. A constructor parameter of type IChocolateDispenser does not receive a test parameter of a type that derives from it.
  • A constructor parameter of a delegate or abstract class type only gets a mock from a test parameter of that type.
  • When two classes under test depend on each other, only the first of the two test parameters receives the other one; the second gets a new instance or its default value.
  • The given values must have the types of the parameters, e.g. 4 for an int and 4L for a long. [AutoMockArguments(null)] passes null to the first parameter. A single array, e.g. [AutoMockArguments(new object[] { "Dark", 4 })] or [AutoMockArguments(new[] { "Dark", "White" })], passes its elements as the values; to pass the array itself, wrap it, e.g. [AutoMockArguments(new object[] { new[] { "Dark", "White" } })].
  • The Mockolate0010 analyzer reports parameters that Mockolate cannot create, such as IRepository<T> in a generic test.
  • On .NET Framework and .NET Standard, Mockolate declares the ModuleInitializerAttribute when neither the project, a referenced assembly, TUnit nor PolySharp does. If another source generator declares it, set <MockolateUnionAttributePolyfills>ModuleInitializerAttribute</MockolateUnionAttributePolyfills>. If the project excludes the analyzer assets of TUnit (ExcludeAssets="analyzers"), so that its source generator does not run, set <EnableTUnitPolyfills>false</EnableTUnitPolyfills>.