FastMoq 1.22.809.1253

There is a newer version of this package available.
See the version list below for details.
dotnet add package FastMoq --version 1.22.809.1253
                    
NuGet\Install-Package FastMoq -Version 1.22.809.1253
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FastMoq" Version="1.22.809.1253" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FastMoq" Version="1.22.809.1253" />
                    
Directory.Packages.props
<PackageReference Include="FastMoq" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FastMoq --version 1.22.809.1253
                    
#r "nuget: FastMoq, 1.22.809.1253"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=FastMoq&version=1.22.809.1253
                    
Install as a Cake Addin
#tool nuget:?package=FastMoq&version=1.22.809.1253
                    
Install as a Cake Tool

FastMoq

Easy and fast extension of the Moq mocking framework for mocking and auto injection of classes.

Features

  • Test without declaring Mocks (unless needed).
  • Injection: Automatically determines what interfaces need to be injected into the constructor and creates mocks if they do not exist.
    • 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.
  • 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.

Targets

  • .NET Core 3.1
  • .NET 5
  • .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.

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.

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());

Additional Documentation

FastMoq Documentation

Breaking Change

  • 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 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
3.0.0 229 5/12/2025
2.28.3 198 10/18/2024
2.28.2 120 10/17/2024
2.27.6 134 8/1/2024
2.27.5 112 7/29/2024
2.27.4 252 6/27/2024
2.27.3 164 6/13/2024
2.27.2 152 5/30/2024
2.27.1 126 5/29/2024
2.27.0 138 5/28/2024
2.25.0 190 4/10/2024
2.24.300 363 2/1/2024
2.23.201 608 12/13/2023
2.23.200 485 12/2/2023
2.23.124 502 11/7/2023
2.23.123 477 11/3/2023
2.23.122 540 10/11/2023
2.23.120 593 8/31/2023
2.23.118 742 5/24/2023
2.23.117 668 5/4/2023
2.23.116 756 3/31/2023
2.23.115 843 1/14/2023
2.23.113.2138 814 1/13/2023
2.22.1215.1748 866 12/15/2022
1.22.1128.2310 899 11/28/2022
1.22.1117.1322 896 11/17/2022
1.22.1113.29 889 11/13/2022
1.22.1111.1837 889 11/11/2022
1.22.831.1343 969 8/31/2022
1.22.810.1759 967 8/10/2022
1.22.809.1253 977 8/9/2022
1.22.805.1334 1,005 8/5/2022
1.22.728.1251 982 7/28/2022
1.22.727.1847 947 7/27/2022
1.22.604.1441 986 6/4/2022
1.22.531.1514 1,003 5/31/2022
1.22.531.1336 949 5/31/2022
1.22.529.1812 959 5/29/2022
1.0.0 443 11/3/2023