, , AddDbContext services, .
, .
Startup class - . , , ( ASP.NET Core).
, :
public class TestController : Controller
{
public TestController(ISomeDependency dependency)
{
}
}
, Startup ISomeDependency. ISomeDependency , .
, , , . .
, , . , .
Unit Test, - . , , . , . , , .
:
[TestMethod]
public void ConfigureServices_RegistersDependenciesCorrectly()
{
Mock<IConfigurationSection> configurationSectionStub = new Mock<IConfigurationSection>();
configurationSectionStub.Setup(x => x["DefaultConnection"]).Returns("TestConnectionString");
Mock<Microsoft.Extensions.Configuration.IConfiguration> configurationStub = new Mock<Microsoft.Extensions.Configuration.IConfiguration>();
configurationStub.Setup(x => x.GetSection("ConnectionStrings")).Returns(configurationSectionStub.Object);
IServiceCollection services = new ServiceCollection();
var target = new Startup(configurationStub.Object);
target.ConfigureServices(services);
services.AddTransient<TestController>();
var serviceProvider = services.BuildServiceProvider();
var controller = serviceProvider.GetService<TestController>();
Assert.IsNotNull(controller);
}