Research: Mocking IConfiguration from .NET Core
I need to integration test my data access layer to ensure that all the code is working properly.
I know that it isn't going to work using the normal way:
//Will return a NotSupportedException
var mock = new Mock<IConfiguration>();
mock.Setup(arg => arg.GetConnectionString(It.IsAny<string>()))
.Returns("testDatabase");
Normally the data access layer uses dependency injection and it retrieves the connection string with IConfiguration
.
My integration test:
[Fact]
public async void GetOrderById_ScenarioReturnsCorrectData_ReturnsTrue()
{
// Arrange
OrderDTO order = new OrderDTO();
// Mocking the ASP.NET IConfiguration for getting the connection string from appsettings.json
var mockConfSection = new Mock<IConfigurationSection>();
mockConfSection.SetupGet(m => m[It.Is<string>(s => s == "testDB")]).Returns("mock value");
var mockConfiguration = new Mock<IConfiguration>();
mockConfiguration.Setup(a => a.GetSection(It.Is<string>(s => s == "ConnectionStrings:testDB"))).Returns(mockConfSection.Object);
IDataAccess dataAccess = new SqlDatabase(mockConfiguration.Object);
IRepository repository = new repository(dataAccess, connectionStringData);
var connectionStringData = new ConnectionStringData
{
SqlConnectionLocation = "testDatabase"
};
// Act
int id = await repository.CreateOrder(order);
// Assert
Assert.Equal(1, id);
}
But I get an error
System.InvalidOperationException: The ConnectionString property has not been initialized.
I'm a bit lost here, I'm not sure what happened.
Try changing :
mockConfiguration.Setup(a => a.GetSection(It.Is<string>(s => s == "ConnectionStrings:testDB"))).Returns(mockConfSection.Object);
To:
mockConfiguration.Setup(a => a.GetSection(It.Is<string>(s => s == "ConnectionStrings"))).Returns(mockConfSection.Object);
Next setup prints "mock value":
var mockConfSection = new Mock<IConfigurationSection>();
mockConfSection.SetupGet(m => m[It.Is<string>(s => s == "testDB")]).Returns("mock value");
var mockConfiguration = new Mock<IConfiguration>();
mockConfiguration.Setup(a => a.GetSection(It.Is<string>(s => s == "ConnectionStrings"))).Returns(mockConfSection.Object);
Console.WriteLine(mockConfiguration.Object.GetConnectionString("testDB")); // prints "mock value"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With