Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Replace type in ServiceCollection for testing
public static class ServiceExtensions
{
public static ServiceCollection ReplaceWithFake<T>(this ServiceCollection services) where T : class
{
T fake = Substitute.For<T>();
var descriptor = new ServiceDescriptor(typeof(T), fake.GetType(), ServiceLifetime.Scoped);
services.Replace(descriptor);
return services;
}
}
// Usage: (install NSubstitute from Nuget)
//services.ReplaceWithFake<ILogger>()
// .ReplaceWithFake<IRepository>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.