mcbain
mcbain
CC#
Created by mcbain on 8/17/2023 in #help
❔ Assistance required trying to create a generic base class for unit tests for mocked services
For some context, I started with a net 6 console app (you know the template that just acts as Main?) and moved it into a Program, Startup style project. I'm using Mediatr as CQRS but my Program starts like this and then continues with some Mediator handlers:
IServiceCollection services = new ServiceCollection();

Startup startup = new Startup();
startup.ConfigureServices(services);
IServiceProvider serviceProvider = services.BuildServiceProvider();

_consoleService = serviceProvider
.GetService<IConsoleService>();
IServiceCollection services = new ServiceCollection();

Startup startup = new Startup();
startup.ConfigureServices(services);
IServiceProvider serviceProvider = services.BuildServiceProvider();

_consoleService = serviceProvider
.GetService<IConsoleService>();
and my Startup uses the IServiceCollection to register the services much like a web API project setup. I'm looking to create a new base class and inside [TestInitialize] create a new Startup and replace the service collection with mocked services. I've scoured Google for some examples but I don't think I'm searching for the right thing.. or the base examples are too simplified for this scenario. P.S: I've just started using NSubstitute following the whole Moq debacle which I'm more familiar with so there may be some fundamentals I'm missing here Appreciate any help
2 replies