Reputation: 11
my post is an extension of the post How to Unit Test Startup.cs in .NET Core
I tried to unit test the class Startup.ca using this(same code then the previous post):
[TestMethod]
public void ConfigureServices_RegistersDependenciesCorrectly()
{
// Arrange
// Setting up the stuff required for Configuration.GetConnectionString("DefaultConnection")
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);
// Act
target.ConfigureServices(services);
// Mimic internal asp.net core logic.
services.AddTransient<TestController>();
// Assert
var serviceProvider = services.BuildServiceProvider();
var controller = serviceProvider.GetService<TestController>();
Assert.IsNotNull(controller);
}
But i got an error at the line target.ConfigureServices(services); when i run the test. I got the error:
Message: Test method Project.Services.PublicReporting.Tests.StartupTests.ConfigureServices_RegistersDependenciesCorrectly threw exception: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Caching.Memory, Version=2.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) ---> System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Caching.Memory, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
It's because we are calling ConfigureService and it crash at services.AddResponseCaching();
This is my Configures Service method, it crash as the second line.
public void ConfigureServices(IServiceCollection services)
{
services.RegisterApplicationComponents();
services.AddResponseCaching();
}
I think the problem is when we mock the class, i can't find out, which proper setting we need to had so the AddResponseCaching() method will pass. Does anybody know what is missing in the configurationSectionStub.Setup or maybe we need to mock the behavior of the service?
thanks!
Upvotes: 1
Views: 10390
Reputation: 28290
Use Integration tests in ASP.NET Core for make sure Startup
infrastructure works fine. And note you can inject mock services by overridding treal ones in a test with using ConfigureTestServices
on the host builder.
[Fact]
public async Task Get_QuoteService_ProvidesQuoteInPage()
{
// Arrange
var client = _factory.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
services.AddScoped<IQuoteService, TestQuoteService>();
});
})
.CreateClient();
//Act
var defaultPage = await client.GetAsync("/");
var content = await HtmlHelpers.GetDocumentAsync(defaultPage);
var quoteElement = content.QuerySelector("#quote");
// Assert
Assert.Equal("Something's interfering with time, Mr. Scarman, " +
"and time is my business.", quoteElement.Attributes["value"].Value);
}
Upvotes: 4