Skip to content

YarinOmesi/MockFiller

Repository files navigation

Mock Filler ⭐

Build

Creating tested class instance with mocks!

Refer to Test File Example to see an example, Or the tests Source Generator Tests.

How To Use

All you need to do is to mark your test fixture class as partial.

Create field of the desired tested class and mark it with attribute [FillMocks] or [FillMocksWithWrappers].

The Source Generator will Create a field for each mocked parameters with the same name, and create a Build() method to create the instance.

❗ Note: Currently The Method Overload With More Parameters will be used.

Features

FillMocks

To Fill Mocks for given tested class, create a field for that class and mark it with [FillMocks]

[FillMocks]
private TestedClass _testedClass;

To access a mock of parameter named loggerFactory

_loggerFactory.Mock // Mock<ILoggerFactory>

Default Value

To declare a default value instead of creating a mock, mark a field with [DefaultValue("FieldName")] attribute.

Example For setting a default value for parameter named factory:

[DefaultValue("factory")]
private ILoggerFactory _nullLoggerFactory = NullLoggerFactory.Instance;

Generate Mock Wrappers 🔮

Generate mock wrappers by marking the test class field with [FillMocksWithWrappers] attribute instead of [FillMocks].

The mocked parameter field will have a property for each public method in the mocked type.

A Setup and Verify methods will be generated for each public method of the mocked type.

Demonstration

Using Setup

original

_dependencyMock.Setup(dependency => dependency.MakeString(It.IsAny<int>()))
    .Returns<int>((number) => number.ToString());

With MockFiller

_dependency.MakeString.Setup()
    .Returns<int>(n=> n.ToString());
Using Verify

original

_dependencyMock.Verify(dependency => dependency.MakeString(It.IsAny<int>()), Times.Once)

With MockFiller

_dependency.MakeString.Verify(Times.Once())

Examples

Examples Of Mocked Type IDependency When Generating Wrappers And When Don't.

Attribute Example Link
[FillMocks] IDependency without wrappers
[FillMocksWithWrappers] IDependency with wrappers