Table of Contents

Class WebApplicationFactoryTimeExtensions

Namespace
Trellis.Testing
Assembly
Trellis.Testing.dll

Extension methods for WebApplicationFactory<TEntryPoint> that simplify controlling time in integration tests.

public static class WebApplicationFactoryTimeExtensions
Inheritance
WebApplicationFactoryTimeExtensions
Inherited Members

Methods

WithFakeTimeProvider<TEntryPoint>(WebApplicationFactory<TEntryPoint>, FakeTimeProvider)

Returns a new WebApplicationFactory<TEntryPoint> that replaces the TimeProvider singleton with a Microsoft.Extensions.Time.Testing.FakeTimeProvider. Tests can rewind/advance time via fakeTimeProvider to control timestamps set by domain logic and EntityTimestampInterceptor.

public static WebApplicationFactory<TEntryPoint> WithFakeTimeProvider<TEntryPoint>(this WebApplicationFactory<TEntryPoint> factory, FakeTimeProvider fakeTimeProvider) where TEntryPoint : class

Parameters

factory WebApplicationFactory<TEntryPoint>

The web application factory.

fakeTimeProvider FakeTimeProvider

The shared Microsoft.Extensions.Time.Testing.FakeTimeProvider instance that tests use to control time.

Returns

WebApplicationFactory<TEntryPoint>

A new factory with the fake time provider registered.

Type Parameters

TEntryPoint

The entry point class of the web application under test.

Examples

var fakeTime = new FakeTimeProvider(DateTimeOffset.UtcNow);
_factory = _factory.WithFakeTimeProvider(fakeTime);

fakeTime.SetUtcNow(DateTimeOffset.UtcNow.AddDays(-8));
await client.PostAsync("/api/orders/1/submission", null, ct);

fakeTime.SetUtcNow(DateTimeOffset.UtcNow);
var response = await client.GetAsync("/api/orders/overdue", ct);

Remarks

The Microsoft.Extensions.Time.Testing.FakeTimeProvider is registered as a singleton, so all scopes (including per-request scopes in the HTTP pipeline) share the same clock.

For full time control including EF Core interceptors, also wire AddTrellisInterceptors(fakeTimeProvider) via ReplaceDbProvider<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>).

WithFakeTimeProvider<TEntryPoint>(WebApplicationFactory<TEntryPoint>, out FakeTimeProvider)

Returns a new WebApplicationFactory<TEntryPoint> with a Microsoft.Extensions.Time.Testing.FakeTimeProvider registered, and outputs the provider for test use.

public static WebApplicationFactory<TEntryPoint> WithFakeTimeProvider<TEntryPoint>(this WebApplicationFactory<TEntryPoint> factory, out FakeTimeProvider fakeTimeProvider) where TEntryPoint : class

Parameters

factory WebApplicationFactory<TEntryPoint>

The web application factory.

fakeTimeProvider FakeTimeProvider

When this method returns, contains the Microsoft.Extensions.Time.Testing.FakeTimeProvider instance.

Returns

WebApplicationFactory<TEntryPoint>

A new factory with the fake time provider registered.

Type Parameters

TEntryPoint

The entry point class of the web application under test.