MockAsyncEnumerable 3.0.0.3872

dotnet add package MockAsyncEnumerable --version 3.0.0.3872
                    
NuGet\Install-Package MockAsyncEnumerable -Version 3.0.0.3872
                    
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="MockAsyncEnumerable" Version="3.0.0.3872" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MockAsyncEnumerable" Version="3.0.0.3872" />
                    
Directory.Packages.props
<PackageReference Include="MockAsyncEnumerable" />
                    
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 MockAsyncEnumerable --version 3.0.0.3872
                    
#r "nuget: MockAsyncEnumerable, 3.0.0.3872"
                    
#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 MockAsyncEnumerable@3.0.0.3872
                    
#: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=MockAsyncEnumerable&version=3.0.0.3872
                    
Install as a Cake Addin
#tool nuget:?package=MockAsyncEnumerable&version=3.0.0.3872
                    
Install as a Cake Tool

A simple implementation for transforming synchronous collections into async enumerables, enabling seamless testing of EF Core queries, paginated grids, and LINQ operations, or any action or dynamic aggregated query using EF Core (Microsoft.EntityFrameworkCore) through Expressions (System.Linq.Expressions) with IAsyncEnumerable<T>.

This library was born from the necessity to implement paged grid results in projects where data was provided by stored procedures or in-memory collections, while still needing to support async EF Core APIs like ToListAsync(), FirstOrDefaultAsync(), etc.

Root namespace: RzR.Extensions.EntityMock (with sub-namespaces .Abstractions, .Extensions, .Faults, .Helpers).

Features

  • Convert IEnumerable<T>, IQueryable<T>, and arrays to IAsyncEnumerable<T> / IQueryable<T>;
  • Public APIs return the IMockAsyncEnumerable<T> abstraction (combines IAsyncEnumerable<T> + IQueryable<T>);
  • Builder pattern for fluent async enumerable construction;
  • Factory methods for quick creation (Empty, Single, Create);
  • Fault injection on the builder: artificial latency, throw-at-index, and time-based cancellation — useful for testing retry, timeout and cancellation logic;
  • Full support for LINQ async operations (Where, Select, OrderBy, etc.);
  • Comprehensive null guards and error handling;
  • Proper cancellation token support (linked tokens for combined caller / internal cancellation);
  • Optimized disposal patterns (IAsyncDisposable);
  • Multi-target: netstandard2.0, netstandard2.1, net5.0net9.0.

Installation

In case you wish to use it in your project, u can install the package from <a href="https://www.nuget.org/packages/MockAsyncEnumerable" target="_blank">nuget.org</a> or specify what version you want:

Install-Package MockAsyncEnumerable

Or specify a version:

Install-Package MockAsyncEnumerable -Version x.x.x.x

Examples

Using Extension Methods

using RzR.Extensions.EntityMock.Extensions;
using Microsoft.EntityFrameworkCore;

// Convert a list to async enumerable
var users = new List<User>
{
    new User { Id = 1, Name = "Alice" },
    new User { Id = 2, Name = "Bob" }
};

var asyncUsers = users.ToMockAsyncEnumerable();
var result = await asyncUsers.Where(u => u.Id > 1).ToListAsync();

Using Factory Methods

using RzR.Extensions.EntityMock;
using Microsoft.EntityFrameworkCore;

// Create empty async enumerable
var empty = AsyncEnumerableFactory.Empty<User>();

// Create from single item
var single = AsyncEnumerableFactory.Single(new User { Id = 1, Name = "Alice" });

// Create from multiple items
var multiple = AsyncEnumerableFactory.Create(
    new User { Id = 1, Name = "Alice" },
    new User { Id = 2, Name = "Bob" }
);

var users = await multiple.ToListAsync();

Using Builder Pattern

using RzR.Extensions.EntityMock;
using Microsoft.EntityFrameworkCore;

// Build an async enumerable fluently
var users = AsyncEnumerableFactory.Builder<User>()
    .Add(new User { Id = 1, Name = "Alice" })
    .Add(new User { Id = 2, Name = "Bob" })
    .AddRange(moreUsers)
    .Build();

var result = await users.ToListAsync();

Fault Injection (latency, exceptions, cancellation)

The builder can inject realistic failure modes so tests can exercise retry, timeout and cancellation paths in production code without changing it:

using RzR.Extensions.EntityMock;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

var slow = AsyncEnumerableFactory.Builder<User>()
    .AddRange(users)
    .WithDelay(TimeSpan.FromMilliseconds(50)) // per-item latency
    .ThrowAfter(3, _ => new DbUpdateException("forced")) // throw at index 3
    .CancelAfter(TimeSpan.FromSeconds(1)) // internal cancel
    .Build();

// Caller-supplied cancellation token is honored alongside the internal one.
await foreach (var u in slow.WithCancellation(ct)) { /* ... */ }

Available builder fault APIs:

Method Description
WithDelay(TimeSpan) Awaits the delay before every MoveNextAsync call
ThrowAfter(int, Exception) Throws the supplied exception when the given index is reached
ThrowAfter(int, Func<int, Exception>) Same, but the factory receives the triggering index
CancelAfter(TimeSpan) Starts an internal CancellationTokenSource linked with the caller token

Using EnumerableInvoker (deprecated)

!!! EnumerableInvoker is marked [Obsolete] and will be removed in the next major version. Prefer AsyncEnumerableFactory, the ToMockAsyncEnumerable() extension methods, or AsyncEnumerableBuilder<T>.

using RzR.Extensions.EntityMock;
using Microsoft.EntityFrameworkCore;

var data = new List<User>
{
    new User { Id = 1, Name = "Alice" },
    new User { Id = 2, Name = "Bob" }
};

#pragma warning disable CS0618
var asyncEnumerable = EnumerableInvoker.Invoke(data);
#pragma warning restore CS0618

var result = await asyncEnumerable.FirstOrDefaultAsync(u => u.Name == "Alice");

API Reference

All factory / builder / extension methods now return IMockAsyncEnumerable<T> (defined in RzR.Extensions.EntityMock.Abstractions), which extends both IAsyncEnumerable<T> and IQueryable<T>.

Abstraction (RzR.Extensions.EntityMock.Abstractions)

Type Description
IMockAsyncEnumerable<T> Public, stable surface — combines IAsyncEnumerable<T> + IQueryable<T>

Extension Methods (RzR.Extensions.EntityMock.Extensions)

Method Description
ToMockAsyncEnumerable<T>(this IEnumerable<T>) Converts any enumerable to async enumerable
ToMockAsyncEnumerable<T>(this IQueryable<T>) Converts queryable to async enumerable
ToMockAsyncEnumerable<T>(this T[]) Converts array to async enumerable

Factory Methods (AsyncEnumerableFactory)

Method Description
Empty<T>() Creates an empty async enumerable
Single<T>(T item) Creates async enumerable with single item
Create<T>(params T[] items) Creates async enumerable from items
Builder<T>() Returns a new builder instance

Builder Pattern (AsyncEnumerableBuilder<T>)

Method Description
Add(T item) Adds a single item (fluent)
AddRange(IEnumerable<T> items) Adds multiple items (fluent)
WithDelay(TimeSpan delay) Configures per-item async latency (fluent)
ThrowAfter(int, Exception) Throws at the configured zero-based index (fluent)
ThrowAfter(int, Func<int, Exception>) Same, with index-aware factory (fluent)
CancelAfter(TimeSpan) Cancels iteration after the duration (fluent)
Build() Snapshots items and creates the async enumerable
Clear() Resets items and fault settings

Classic Approach (EnumerableInvoker) — deprecated

Method Description
Invoke<T>(IEnumerable<T>) [Obsolete] — use ToMockAsyncEnumerable() instead
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 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 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 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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MockAsyncEnumerable:

Package Downloads
PagedListResult

An extension that offers the possibility to get/return data set as a list with pagination with filter, order, and search availability, loading the list with page size. Available methods: GetPaged/Async, GetPagedWithMainFiltersAsync, GetPagedWithFiltersAsync.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.0.3872 38 4/24/2026
2.0.0.6582 167 3/13/2026
1.1.0 830 1/10/2025
1.0.3.6469 9,981 12/27/2023
1.0.2.1050 3,091 11/9/2023
1.0.1.1250 977 10/19/2023
1.0.0 2,628 4/28/2023