Creating tested class instance with mocks!
Refer to Test File Example to see an example, Or the tests Source Generator Tests.
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.
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>
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 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.
original
_dependencyMock.Setup(dependency => dependency.MakeString(It.IsAny<int>()))
.Returns<int>((number) => number.ToString());
With MockFiller
_dependency.MakeString.Setup()
.Returns<int>(n=> n.ToString());
original
_dependencyMock.Verify(dependency => dependency.MakeString(It.IsAny<int>()), Times.Once)
With MockFiller
_dependency.MakeString.Verify(Times.Once())
Examples Of Mocked Type IDependency When Generating Wrappers And When Don't.
Attribute | Example Link |
---|---|
[FillMocks] |
IDependency without wrappers |
[FillMocksWithWrappers] |
IDependency with wrappers |