FastMoq.Abstractions 4.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package FastMoq.Abstractions --version 4.0.2
                    
NuGet\Install-Package FastMoq.Abstractions -Version 4.0.2
                    
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.Abstractions" Version="4.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FastMoq.Abstractions" Version="4.0.2" />
                    
Directory.Packages.props
<PackageReference Include="FastMoq.Abstractions" />
                    
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.Abstractions --version 4.0.2
                    
#r "nuget: FastMoq.Abstractions, 4.0.2"
                    
#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.
#:package FastMoq.Abstractions@4.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FastMoq.Abstractions&version=4.0.2
                    
Install as a Cake Addin
#tool nuget:?package=FastMoq.Abstractions&version=4.0.2
                    
Install as a Cake Tool

FastMoq

FastMoq is a .NET testing framework for auto-mocking, dependency injection, and test-focused object creation. In v4, it supports a provider-first architecture with a bundled reflection default and optional Moq compatibility when you need it.

Start Here

  • If you are evaluating the project for the first time, start with Why FastMoq, Packages, and the documentation links below.
  • If you want the shortest path to writing a test, go straight to Getting Started Guide.
  • If you are upgrading from the 3.0.0 line, go straight to Migration Guide.

Release Highlights Since 3.0.0

  • provider-first architecture built around IMockingProvider, IFastMock<T>, and MockingProviderRegistry
  • new package split with FastMoq.Abstractions, FastMoq.Database, FastMoq.Provider.Moq, and FastMoq.Provider.NSubstitute
  • provider-neutral verification with Verify(...), VerifyLogged(...), and TimesSpec
  • fluent scenario building through Scenario.With(...).When(...).Then(...).Verify(...)
  • explicit construction, invocation, and known-type policies instead of older coupled option bags
  • expanded migration docs, executable examples, and generated API coverage

Why FastMoq

FastMoq is still intended to remove boilerplate compared with using a mock provider directly.

The value is not only shorter setup calls. The bigger value is that FastMoq keeps the repetitive test harness work out of the test body:

  • no separate mock field for every constructor dependency
  • no manual new Mock<T>() declarations just to build the subject under test
  • no long constructor call that must be updated every time the component gains a new dependency
  • built-in support for common framework-heavy test types such as ILogger, IFileSystem, HttpClient, and DbContext
  • tracked mocks plus provider-neutral verification and logging helpers

FastMoq vs Direct Mock Provider Usage

Using a mock provider directly:

public class OrderProcessingServiceTests
{
    [Fact]
    public async Task PlaceOrderAsync_ShouldPersistAndLog_WhenReservationAndPaymentSucceed()
    {
        var inventoryGateway = new Mock<IInventoryGateway>();
        var paymentGateway = new Mock<IPaymentGateway>();
        var orderRepository = new Mock<IOrderRepository>();
        var logger = new Mock<ILogger<OrderProcessingService>>();

        var component = new OrderProcessingService(
            inventoryGateway.Object,
            paymentGateway.Object,
            orderRepository.Object,
            logger.Object);

        inventoryGateway
            .Setup(x => x.ReserveAsync("SKU-RED-CHAIR", 2, CancellationToken.None))
            .ReturnsAsync(true);

        paymentGateway
            .Setup(x => x.ChargeAsync("cust-42", 149.90m, CancellationToken.None))
            .ReturnsAsync("pay_12345");

        var result = await component.PlaceOrderAsync(new OrderRequest
        {
            CustomerId = "cust-42",
            Sku = "SKU-RED-CHAIR",
            Quantity = 2,
            TotalAmount = 149.90m,
        }, CancellationToken.None);

        result.Success.Should().BeTrue();
        orderRepository.Verify(x => x.SaveAsync(It.IsAny<OrderRecord>(), CancellationToken.None), Times.Once);
    }
}

Using FastMoq:

public class OrderProcessingServiceTests : MockerTestBase<OrderProcessingService>
{
    [Fact]
    public async Task PlaceOrderAsync_ShouldPersistAndLog_WhenReservationAndPaymentSucceed()
    {
        Mocks.GetOrCreateMock<IInventoryGateway>()
            .Setup(x => x.ReserveAsync("SKU-RED-CHAIR", 2, CancellationToken.None))
            .ReturnsAsync(true);

        Mocks.GetOrCreateMock<IPaymentGateway>()
            .Setup(x => x.ChargeAsync("cust-42", 149.90m, CancellationToken.None))
            .ReturnsAsync("pay_12345");

        var result = await Component.PlaceOrderAsync(new OrderRequest
        {
            CustomerId = "cust-42",
            Sku = "SKU-RED-CHAIR",
            Quantity = 2,
            TotalAmount = 149.90m,
        }, CancellationToken.None);

        result.Success.Should().BeTrue();
        Mocks.Verify<IOrderRepository>(x => x.SaveAsync(It.IsAny<OrderRecord>(), CancellationToken.None), TimesSpec.Once);
        Mocks.VerifyLogged(LogLevel.Information, "Placed order");
    }
}

The FastMoq version removes explicit mock declarations, subject construction, and logger-plumbing code while still allowing provider-specific setup when you need it.

πŸ“š Documentation

Additional Resources

  • πŸ“– Complete Documentation - All guides and references in one place
  • πŸ—ΊοΈ Roadmap Notes - Current provider-first direction and deferred backlog items
  • πŸ†• What's New Since 3.0.0 - Summary of the major post-3.0.0 architecture, packaging, and API changes
  • ⚠️ Breaking Changes - Intentional v4 behavior changes relative to the 3.0.0 public release
  • πŸ”„ Migration Guide - Practical old-to-new guidance from 3.0.0 to the current repo direction
  • ❓ FAQs - Frequently asked questions and troubleshooting
  • πŸ”— API Documentation - Generated HTML API reference

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 through the optional FastMoq.Database package, with the primary calls staying in the FastMoq namespace.
  • Supports Null method parameter testing.
  • Comprehensive Documentation - Complete guides, samples, and real-world patterns.
  • Lower Test Boilerplate - Less fixture code than manual mock declaration plus manual subject construction.

Packages

  • FastMoq - Aggregate package that combines the primary FastMoq runtime, database helpers, and web support.
  • FastMoq.Abstractions - Shared provider contracts used by core and provider packages.
  • FastMoq.Core - Core testing Mocker and provider-first resolution pipeline.
  • FastMoq.Database - Entity Framework and DbContext-focused helpers.
  • FastMoq.Provider.Moq - Moq compatibility provider and Moq-specific convenience extensions for v4 migration.
  • FastMoq.Provider.NSubstitute - Optional NSubstitute provider package.
  • FastMoq.Web - Blazor and web support.

In the current v4 layout, FastMoq.Core bundles the internal reflection provider and the bundled moq compatibility provider. The default provider is reflection. Additional providers such as nsubstitute can be added explicitly.

Typical split-package install:

dotnet add package FastMoq.Core
dotnet add package FastMoq.Database
dotnet add package FastMoq.Web

GetMockDbContext<TContext>() keeps the same main call shape in the FastMoq namespace. If you install FastMoq, the EF helpers are included. If you install FastMoq.Core directly, add FastMoq.Database for DbContext support.

GetMockDbContext<TContext>() remains the default mocked-sets entry point. For explicit mode selection between mocked DbSets and a real EF in-memory context, use GetDbContextHandle<TContext>(new DbContextHandleOptions<TContext> { ... }).

The mocked-sets path is still backed by the existing Moq-based DbContextMock<TContext> implementation, while the real-context path is exposed through DbContextTestMode.RealInMemory.

If you are upgrading an older suite that still uses GetMock<T>(), direct Mock<T> access, VerifyLogger(...), or older HTTP setup helpers such as SetupHttpMessage(...), select Moq explicitly for that test assembly. If you are writing new or actively refactoring tests, prefer provider-neutral APIs such as GetOrCreateMock(...), Verify(...), VerifyLogged(...), WhenHttpRequest(...), and WhenHttpRequestJson(...). When you still need provider-specific setup in v4, use the provider-package extensions on IFastMock<T> such as AsMoq(), Setup(...), or AsNSubstitute().

For the moved HTTP compatibility helpers, the migration point is package selection rather than namespace churn: the Moq compatibility methods remain in the FastMoq.Extensions namespace for low-churn source migration, but their implementation now comes from the FastMoq.Provider.Moq package instead of FastMoq.Core.

Provider selection example:

MockingProviderRegistry.Register("moq", MoqMockingProvider.Instance, setAsDefault: true);
var mocker = new Mocker();

For a temporary override in a specific async scope, use MockingProviderRegistry.Push("providerName"). For detailed setup guidance, see Provider Selection Guide.

Targets

  • .NET 9
  • .NET 8
  • .NET 10

Quick Example

public class OrderProcessingServiceTests : MockerTestBase<OrderProcessingService>
{
    [Fact]
    public async Task PlaceOrderAsync_ShouldPersistAndLog_WhenReservationAndPaymentSucceed()
    {
        Mocks.GetOrCreateMock<IInventoryGateway>()
            .Setup(x => x.ReserveAsync("SKU-RED-CHAIR", 2, CancellationToken.None))
            .ReturnsAsync(true);

        Mocks.GetOrCreateMock<IPaymentGateway>()
            .Setup(x => x.ChargeAsync("cust-42", 149.90m, CancellationToken.None))
            .ReturnsAsync("pay_12345");

        var result = await Component.PlaceOrderAsync(new OrderRequest
        {
            CustomerId = "cust-42",
            Sku = "SKU-RED-CHAIR",
            Quantity = 2,
            TotalAmount = 149.90m,
        }, CancellationToken.None);

        result.Success.Should().BeTrue();
        Mocks.Verify<IOrderRepository>(x => x.SaveAsync(It.IsAny<OrderRecord>(), CancellationToken.None), TimesSpec.Once);
        Mocks.VerifyLogged(LogLevel.Information, "Placed order");
    }
}

Common Entry Points

  • MockerTestBase<TComponent>: shortest path for service and component tests with automatic construction and dependency injection
  • Mocker: standalone entry point when you do not want a test base class
  • GetMock<T>(): lowest-churn compatibility path for older Moq-shaped tests in v4
  • GetOrCreateMock<T>(): tracked provider-first mock access for the forward v4 and v5 path
  • Verify(...), VerifyNoOtherCalls(...), VerifyLogged(...), and TimesSpec: provider-neutral verification surface

Learn More

License

License - MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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 is compatible.  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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on FastMoq.Abstractions:

Package Downloads
FastMoq

Complete FastMoq package for automatic test double creation, constructor injection, EF testing helpers, Azure Functions worker helpers, and Blazor/web testing support. Includes the core runtime plus optional framework helper integrations.

FastMoq.Core

Core FastMoq runtime for automatic test double creation, constructor injection, and provider-neutral testing helpers. Uses reflection by default in v4, includes Moq compatibility, and supports additional providers when added explicitly.

FastMoq.Provider.Moq

Moq provider package for FastMoq v4 transition. This bundled dependency will be removed from FastMoq.Core in v5.

FastMoq.Provider.NSubstitute

NSubstitute provider package for FastMoq.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.1.2 172 4/13/2026
4.1.1 160 4/12/2026
4.1.0 169 4/11/2026
4.0.3 169 4/10/2026
4.0.2 149 4/7/2026