Path: redb-identity/tests/redb.Identity.Tests/Infrastructure/MockRouteContext.cs
Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity
Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/Infrastructure/MockRouteContext.cs
----
using NSubstitute;
using Microsoft.Extensions.DependencyInjection;
using redb.Core;
using redb.Route.Abstractions;
namespace redb.Identity.Tests.Infrastructure;
///
/// Creates a mock that resolves the given
/// via context.GetService<IRedbService>() (used by GetRedbService() extension).
///
internal static class MockRouteContext
{
public static IRouteContext Create(IRedbService redb)
{
var context = Substitute.For();
context.GetService().Returns(redb);
// NSubstitute auto-mocks interface return types — pin GetFromRegistry to null so
// GetIdentityService falls through to the host SP instead of an empty auto-mocked scope.
context.GetFromRegistry(Arg.Any()).Returns((IServiceScopeFactory?)null);
return context;
}
///
/// Creates a mock that also exposes a service provider via
/// context.GetServiceProvider(). Use when the processor under test resolves
/// additional services (e.g. IOpenIddictApplicationManager) from the SP.
///
public static IRouteContext CreateWithServices(IRedbService redb, params (Type, object)[] services)
{
var context = Substitute.For();
context.GetService().Returns(redb);
// NSubstitute auto-mocks interface return types — pin GetFromRegistry to null so
// GetIdentityService falls through to the host SP instead of an empty auto-mocked scope.
context.GetFromRegistry(Arg.Any()).Returns((IServiceScopeFactory?)null);
var sc = new ServiceCollection();
sc.AddSingleton(redb);
foreach (var (type, instance) in services)
sc.AddSingleton(type, instance);
var sp = sc.BuildServiceProvider();
context.GetServiceProvider().Returns(sp);
return context;
}
}