Skip to main content

Overview

NuGet.Services.Testing.Entities is a small, focused test-support library that solves a specific pain point: Entity Framework 6’s IDbSet<T> and async LINQ operators (ToListAsync, FirstOrDefaultAsync, etc.) cannot be faked with a plain in-memory list out of the box. EF’s async extension methods require the underlying IQueryProvider to implement IDbAsyncQueryProvider, which a standard List<T> does not satisfy. This library ships three building blocks that wire everything up correctly via Moq, and a high-level convenience extension that configures an entire IValidationEntitiesContext mock in one call.
This project is a test infrastructure library only — it must never be referenced from production code. It targets net472 and is intended for use inside xUnit / MSTest unit test projects within the NuGetGallery solution.

Role in the System

The NuGetGallery solution separates package validation concerns into NuGet.Services.Validation, which defines the EF entity model (IValidationEntitiesContext, PackageValidation, PackageValidationSet, certificate chain entities, etc.). Every service that queries that context needs a way to write fast, isolated unit tests without a real SQL Server connection. NuGet.Services.Testing.Entities sits between those two concerns:
Any test project that validates business logic against the EF validation context can reference this library instead of duplicating the async-mock boilerplate.

Key Files and Classes

Dependencies

NuGet Package References

Internal Project References

There is no direct reference to the EntityFramework NuGet package. The library relies only on the System.Data.Entity and System.Data.Entity.Infrastructure namespaces that are available via the .NET Framework 4.7.2 target and the EF6 assemblies pulled in transitively by NuGet.Services.Validation.

Usage Pattern

Quick context mock

Call .Mock(...) with only the entity collections you care about. All other IDbSet properties are still configured as empty sets, so queries against them do not throw.

Generic DbSet mock

Use SetupDbSet directly when working with custom IDbSet types outside the validation context.

Notable Patterns and Implementation Details

Async shim patternTestDbAsyncQueryProvider and TestDbAsyncEnumerator are a direct adaptation of the Microsoft MSDN guidance for testing EF6 async queries. The source comment in TestDbAsyncQueryProvider.cs acknowledges this origin explicitly. The approach wraps synchronous in-memory LINQ with Task.FromResult(...) calls, which is safe in unit tests because there is no real I/O.
Mutable in-memory collectionIDbSetMockExtensions.SetupDbSet captures the seed data as an IQueryable<TEntity> local variable and re-assigns it inside the Add and Remove Moq callbacks using Concat / Where. This means the mock DbSet supports mutations during a test, but the mutations are not thread-safe and the collection reference is rebuilt on every add/remove.
EF6 only — not compatible with EF CoreSetupDbSet requires TDbSet : IDbSet<TEntity>. EF Core uses DbSet<T> directly and does not implement IDbSet<T>, so this library cannot be used with EF Core contexts. It is strictly an EF 6 / net472 utility.
Namespace placementIDbSetMockExtensions is declared in the System.Data.Entity namespace (not NuGet.Services.Testing.Entities), so consuming test projects pick up the extension methods automatically whenever they have a using System.Data.Entity; directive — no extra using statement is required.

File Inventory