FastMoq.Core
1.0.0
See the version list below for details.
dotnet add package FastMoq.Core --version 1.0.0
NuGet\Install-Package FastMoq.Core -Version 1.0.0
<PackageReference Include="FastMoq.Core" Version="1.0.0" />
paket add FastMoq.Core --version 1.0.0
#r "nuget: FastMoq.Core, 1.0.0"
// Install FastMoq.Core as a Cake Addin #addin nuget:?package=FastMoq.Core&version=1.0.0 // Install FastMoq.Core as a Cake Tool #tool nuget:?package=FastMoq.Core&version=1.0.0
FastMoq
Easy and fast extension of the Moq mocking framework for mocking and auto injection of classes when testing.
API Documentation
Features
- NOW BLAZOR SUPPORT in FastMoq and FastMoq.Web.
- Test without declaring Mocks (unless needed).
- Creates objects with chain of automatic injections in objects and their dependencies.
- Creates Mocks and Objects with properties populated.
- Automatically injects and creates components or services.
- Injection: Automatically determines what interfaces need to be injected into the constructor and creates mocks if they do not exist.
- Generate Mock using specific data.
- Best guess picks the multiple parameter constructor over the default constructor.
- Specific mapping allows the tester to create an instance using a specific constructor and specific data.
- Supports Inject Attributes and multiple constructors.
- Use Mocks without managing fields and properties. Mocks are managed by the Mocker framework. No need to keep track of Mocks. Just use them!
- Create instances of Mocks with non public constructors.
- HttpClient and IFileSystem test helpers
- DbContext support - SqlLite and mockable objects.
- Supports Null method parameter testing.
Packages
- FastMoq - Combines FastMoq.Core and FastMoq.Web. -.NET Core 6.0 and 7.0.
- FastMoq.Core - Original FastMoq testing Mocker. -.NET 6.0, and 7.0.
- FastMoq.Web - New Blazor and Web support. -.NET Core 6.0 and 7.0.
Targets
- .NET 7
- .NET 6
Most used classes in the FastMoq namespace
public class Mocker {} // Primary class for auto mock and injection. This can be used standalone from MockerTestBase using Mocks property on the base class.
public abstract class MockerTestBase<TComponent> where TComponent : class {} // Assists in the creation of objects and provides direct access to Mocker.
Most used classes in the FastMoq.Web.Blazor namespace
public abstract class MockerBlazorTestBase<T> : TestContext, IMockerBlazorTestHelpers<T> where T : ComponentBase // Assists in the creation of Blazor components and provides direct access to Mocker.
Examples
Basic example of the base class creating the Car class and auto mocking ICarService
public class CarTest : MockerTestBase<Car> {
[Fact]
public void TestCar() {
Component.Color.Should().Be(Color.Green);
Component.CarService.Should().NotBeNull();
}
}
public class Car {
public Color Color { get; set; } = Color.Green;
public ICarService CarService { get; }
public Car(ICarService carService) => CarService = carService;
}
public interface ICarService
{
Color Color { get; set; }
ICarService CarService { get; }
bool StartCar();
}
Example of how to set up for mocks that require specific functionality
public class CarTest : MockerTestBase<Car> {
public CarTest() : base(mocks => {
mocks.Initialize<ICarService>(mock => mock.Setup(x => x.StartCar).Returns(true));
}
}
Auto Injection
Auto injection allows creation of components with parameterized interfaces. If an override for creating the component is not specified, the component will be created will the default Mock Objects.
Auto Injection with instance parameters
Additionally, the creation can be overwritten and provided with instances of the parameters. CreateInstance will automatically match the correct constructor to the parameters given to CreateInstance.
Mocks.CreateInstance(new MockFileSystem()); // CreateInstance matches the parameters and types with the Component constructor.
Interface Type Map
When multiple classes derive from the same interface, the Interface Type Map can map with class to use for the given injected interface. The map can also enable mock substitution.
Example of two classes inheriting the same interface
public class TestClassDouble1 : ITestClassDouble {}
public class TestClassDouble2 : ITestClassDouble {}
Mapping
This code maps ITestClassDouble to TestClassDouble1 when testing a component with ITestClassDouble.
Mocker.AddType<ITestClassDouble, TestClassDouble1>();
The map also accepts parameters to tell it how to create the instance.
Mocks.AddType<ITestClassDouble, TestClassDouble1>(() => new TestClassDouble());
DbContext Mocking
Test ContextDb
public class DbContextTests : MockerTestBase<MyDbContext>
Use ContextDb in test methods
var mockDbContext = Mocks.GetMockDbContext<MyDbContext>();
var dbContext = mockDbContext.Object;
Null / Parameter checking
Testing constructor parameters is very easy with TestAllConstructorParameters and TestConstructorParameters.
TestConstructorParameters - Test the constructor that MockerTestBase used to create the Component for the test. TestAllConstructorParameters - Test all constructors in the component, regardless if the constructor was used to create the component for the test.
Example: Check values for null
// Check values for null
[Fact]
public void Service_NullArgChecks() => TestConstructorParameters((action, constructorName, parameterName) =>
{
output?.WriteLine($"Testing {constructorName}\n - {parameterName}");
action
.Should()
.Throw<ArgumentNullException>()
.WithMessage($"*{parameterName}*");
});
Example: Check values for specific criteria
// Check values for specific criteria.
[Fact]
public void Service_NullArgChecks() => TestConstructorParameters((action, constructorName, parameterName) =>
{
output?.WriteLine($"Testing {constructorName}\n - {parameterName}");
action
.Should()
.Throw<ArgumentNullException>()
.WithMessage($"*{parameterName}*");
},
info =>
{
return info switch
{
{ ParameterType: { Name: "string" }} => string.Empty,
{ ParameterType: { Name: "int" }} => -1,
_ => default,
};
},
info =>
{
return info switch
{
{ ParameterType: { Name: "string" }} => "Valid Value",
{ ParameterType: { Name: "int" }} => 22,
_ => Mocks.GetObject(info.ParameterType),
};
}
);
Example: Test constructors for null with output
// Test constructors for null, using built-in extension and log the output.
[Fact]
public void TestAllConstructors_WithExtension()
{
var messages = new List<string>();
TestAllConstructorParameters((action, constructor, parameter) => action.EnsureNullCheckThrown(parameter, constructor, messages.Add));
messages.Should().Contain(new List<string>()
{
"Testing .ctor(IFileSystem fileSystem, String field)\n - fileSystem",
"Passed fileSystem",
"Testing .ctor(IFileSystem fileSystem, String field)\n - field",
"Passed field",
}
);
}
Additional Documentation
Breaking Change
- 2.23.Latest ⇒ Removed support for .NET Core 5.
- 2.22.1215 ⇒ Removed support for .NET Core 3.1 in FastMoq.Core. Deprecated .NET Core 5 and moved package supporting .NET Core 5.0 from FastMoq to FastMoq.Core.
- 1.22.810 ⇒ Removed setters on the MockerTestBase virtual methods: SetupMocksAction, CreateComponentAction, CreatedComponentAction
- 1.22.810 ⇒ Update Package Dependencies
- 1.22.728 ⇒ Initialize method will reset the mock, if it already exists. This is overridable by settings the reset parameter to false.
- 1.22.604 ⇒ Renamed Mocks to Mocker, Renamed TestBase to MockerTestBase.
License - MIT
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.EntityFrameworkCore.Sqlite.Core (>= 6.0.24)
- Microsoft.Extensions.Http (>= 6.0.0)
- Moq (>= 4.18.4)
- TestableIO.System.IO.Abstractions.TestingHelpers (>= 19.2.69)
-
net7.0
- Microsoft.EntityFrameworkCore.Sqlite.Core (>= 7.0.13)
- Microsoft.Extensions.Http (>= 7.0.0)
- Moq (>= 4.18.4)
- TestableIO.System.IO.Abstractions.TestingHelpers (>= 19.2.69)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on FastMoq.Core:
Package | Downloads |
---|---|
FastMoq
Encapsulates all of FastMoq Core and extensions Blazor/Web Support. Easy and fast extension of the famous Moq mocking framework for mocking and auto injection of classes when testing. Now with Blazor support. |
|
FastMoq.Web
Easy and fast extension of the famous Moq mocking framework for mocking and auto injection of classes when testing. Now with Blazor support. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.28.3 | 148 | 10/18/2024 |
2.28.2 | 99 | 10/17/2024 |
2.27.6 | 143 | 8/1/2024 |
2.27.5 | 87 | 7/29/2024 |
2.27.4 | 232 | 6/27/2024 |
2.27.3 | 151 | 6/13/2024 |
2.27.2 | 169 | 5/30/2024 |
2.27.1 | 131 | 5/29/2024 |
2.27.0 | 146 | 5/28/2024 |
2.25.0 | 199 | 4/10/2024 |
2.24.300 | 310 | 2/1/2024 |
2.23.201 | 452 | 12/13/2023 |
2.23.200 | 379 | 12/2/2023 |
2.23.124 | 385 | 11/7/2023 |
2.23.123 | 370 | 11/3/2023 |
2.23.122 | 399 | 10/11/2023 |
2.23.121 | 369 | 10/11/2023 |
2.23.120 | 434 | 8/31/2023 |
2.23.119 | 418 | 8/31/2023 |
2.23.118 | 519 | 5/24/2023 |
2.23.117 | 482 | 5/4/2023 |
2.23.116 | 599 | 3/31/2023 |
2.23.115 | 728 | 1/14/2023 |
2.23.113.2138 | 323 | 1/13/2023 |
2.22.1215.1757 | 806 | 12/15/2022 |
1.0.0 | 801 | 11/3/2023 |