FastMoq 1.22.604.1441
See the version list below for details.
dotnet add package FastMoq --version 1.22.604.1441
NuGet\Install-Package FastMoq -Version 1.22.604.1441
<PackageReference Include="FastMoq" Version="1.22.604.1441" />
paket add FastMoq --version 1.22.604.1441
#r "nuget: FastMoq, 1.22.604.1441"
// Install FastMoq as a Cake Addin #addin nuget:?package=FastMoq&version=1.22.604.1441 // Install FastMoq as a Cake Tool #tool nuget:?package=FastMoq&version=1.22.604.1441
FastMoq
Easy and fast extension of the Moq mocking framework for mocking and auto injection of classes.
Features
- Auto Injection into tested component constructors
- 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.
- Auto Mocking creation whenever a mock is first used.
Targets
- .NET Core 3.1
- .NET 5
- .NET 6
Available Objects in the FastMoq namespace
public class Mocker {}
public abstract class MockerTestBase<TComponent> where TComponent : class {}
- Mocker is the primary class for auto mock and injection. This can be used standalone from MockerTestBase.
- MockerTestBase assists in the creation of objects and provides direct access to Mocker.
Examples
Example Test Class
Testing this class will auto inject IFileSystem.
public class TestClassNormal : ITestClassNormal
{
public event EventHandler TestEvent;
public IFileSystem FileSystem { get; set; }
public TestClassNormal() { }
public TestClassNormal(IFileSystem fileSystem) => FileSystem = fileSystem;
public void CallTestEvent() => TestEvent?.Invoke(this, EventArgs.Empty);
}
Fast Start Testing
TestClassNormal is created and injects IFileSystem.
public class TestClassNormalTestsDefaultBase : MockerTestBase<TestClassNormal>
{
[Fact]
public void Test1()
{
Component.FileSystem.Should().NotBeNull();
Component.FileSystem.Should().BeOfType<MockFileSystem>();
Component.FileSystem.File.Should().NotBeNull();
Component.FileSystem.Directory.Should().NotBeNull();
}
}
Pre-Test Setup
TestClassNormal is created and injects IFileSystem. SetupMocksAction creates and configures the Mock IFileSystem before the component is created.
public class TestClassNormalTestsSetupBase : MockerTestBase<TestClassNormal>
{
public TestClassNormalTestsSetupBase() : base(SetupMocksAction) { }
private static void SetupMocksAction(Mocker mocks)
{
var iFile = new FileSystem().File;
mocks.Strict = true;
mocks.Initialize<IFileSystem>(mock => mock.Setup(x => x.File).Returns(iFile));
}
[Fact]
public void Test1()
{
Component.FileSystem.Should().NotBeNull();
Component.FileSystem.Should().NotBeOfType<MockFileSystem>();
Component.FileSystem.File.Should().NotBeNull();
Component.FileSystem.Directory.Should().BeNull();
}
}
Custom Setup, Creation, and Post Create routines
TestClassNormal is created and injects IFileSystem. SetupMocksAction creates and configures the Mock IFileSystem before the component is created. Once created, the CreatedComponentAction subscribes to an event on the component.
public class TestClassNormalTestsFull : MockerTestBase<TestClassNormal>
{
private static bool testEventCalled;
public TestClassNormalTestsFull() : base(SetupMocksAction, CreateComponentAction, CreatedComponentAction) => testEventCalled = false;
private static void CreatedComponentAction(TestClassNormal? obj) => obj.TestEvent += (_, _) => testEventCalled = true;
private static TestClassNormal CreateComponentAction(Mocker mocks) => new(mocks.GetObject<IFileSystem>());
private static void SetupMocksAction(Mocker mocks)
{
var mock = new Mock<IFileSystem>();
var iFile = new FileSystem().File;
mocks.Strict = true;
mocks.AddMock(mock, true);
mocks.Initialize<IFileSystem>(xMock => xMock.Setup(x => x.File).Returns(iFile));
}
[Fact]
public void Test1()
{
Component.FileSystem.Should().Be(Mocker.GetMock<IFileSystem>().Object);
Component.FileSystem.Should().NotBeNull();
Component.FileSystem.File.Should().NotBeNull();
Component.FileSystem.Directory.Should().BeNull();
testEventCalled.Should().BeFalse();
Component.CallTestEvent();
testEventCalled.Should().BeTrue();
Mocker.Initialize<IFileSystem>(mock => mock.Setup(x => x.Directory).Returns(new FileSystem().Directory));
Component.FileSystem.Directory.Should().NotBeNull();
}
}
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.
private static TestClassNormal CreateComponentAction() => 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.
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());
Breaking Change
1.22.604 ⇒ Renamed Mocks to Mocker, Renamed TestBase to MockerTestBase.
License - MIT
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. 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 was computed. 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. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Moq (>= 4.17.2)
- System.IO.Abstractions (>= 17.0.15)
- System.IO.Abstractions.TestingHelpers (>= 17.0.15)
-
net5.0
- Moq (>= 4.17.2)
- System.IO.Abstractions (>= 17.0.15)
- System.IO.Abstractions.TestingHelpers (>= 17.0.15)
-
net6.0
- Moq (>= 4.17.2)
- System.IO.Abstractions (>= 17.0.15)
- System.IO.Abstractions.TestingHelpers (>= 17.0.15)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.28.3 | 131 | 10/18/2024 |
2.28.2 | 79 | 10/17/2024 |
2.27.6 | 91 | 8/1/2024 |
2.27.5 | 64 | 7/29/2024 |
2.27.4 | 147 | 6/27/2024 |
2.27.3 | 106 | 6/13/2024 |
2.27.2 | 110 | 5/30/2024 |
2.27.1 | 94 | 5/29/2024 |
2.27.0 | 101 | 5/28/2024 |
2.25.0 | 143 | 4/10/2024 |
2.24.300 | 323 | 2/1/2024 |
2.23.201 | 546 | 12/13/2023 |
2.23.200 | 444 | 12/2/2023 |
2.23.124 | 485 | 11/7/2023 |
2.23.123 | 458 | 11/3/2023 |
2.23.122 | 517 | 10/11/2023 |
2.23.120 | 567 | 8/31/2023 |
2.23.118 | 706 | 5/24/2023 |
2.23.117 | 626 | 5/4/2023 |
2.23.116 | 719 | 3/31/2023 |
2.23.115 | 796 | 1/14/2023 |
2.23.113.2138 | 769 | 1/13/2023 |
2.22.1215.1748 | 827 | 12/15/2022 |
1.22.1128.2310 | 859 | 11/28/2022 |
1.22.1117.1322 | 851 | 11/17/2022 |
1.22.1113.29 | 840 | 11/13/2022 |
1.22.1111.1837 | 849 | 11/11/2022 |
1.22.831.1343 | 916 | 8/31/2022 |
1.22.810.1759 | 916 | 8/10/2022 |
1.22.809.1253 | 928 | 8/9/2022 |
1.22.805.1334 | 957 | 8/5/2022 |
1.22.728.1251 | 918 | 7/28/2022 |
1.22.727.1847 | 899 | 7/27/2022 |
1.22.604.1441 | 935 | 6/4/2022 |
1.22.531.1514 | 944 | 5/31/2022 |
1.22.531.1336 | 898 | 5/31/2022 |
1.22.529.1812 | 903 | 5/29/2022 |
1.0.0 | 422 | 11/3/2023 |