.NET › Dependency Injection
How would you unit test a service that depends on multiple injected interfaces without spinning up the full DI container?
AdvancedAnswer
Unit tests typically construct the service under test directly, passing mock or fake implementations of its dependencies (created via a mocking library like Moq or NSubstitute, or hand-written test doubles) directly to the constructor -- bypassing the DI container entirely, since unit tests should test the class's logic in isolation, not the wiring itself (that's what integration tests using WebApplicationFactory are for).
[Fact]
public async Task ProcessOrder_SendsConfirmationEmail() {
var mockEmailSender = new Mock<IEmailSender>();
var sut = new OrderService(mockEmailSender.Object); // direct construction, no DI container
await sut.ProcessOrderAsync(new Order());
mockEmailSender.Verify(e => e.SendAsync(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}Real-world example A test suite for OrderService directly constructs it with a mocked IEmailSender and IPaymentGateway, verifying business logic behavior in milliseconds without any DI container overhead or database/network dependencies.