如何使用最小起订量从 IConfiguration 模拟 GetConnectionString()?
How to mock GetConnectionString() from IConfiguration using moq?
研究: Mocking IConfiguration from .NET Core
我需要对我的数据访问层进行集成测试,以确保所有代码都能正常工作。
我知道用正常的方式是行不通的:
//Will return a NotSupportedException
var mock = new Mock<IConfiguration>();
mock.Setup(arg => arg.GetConnectionString(It.IsAny<string>()))
.Returns("testDatabase");
通常数据访问层使用依赖注入,它使用 IConfiguration
检索连接字符串。
我的集成测试:
[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);
}
但是我得到一个错误
System.InvalidOperationException: The ConnectionString property has not been initialized.
我有点迷路了,我不确定发生了什么。
尝试更改:
mockConfiguration.Setup(a => a.GetSection(It.Is<string>(s => s == "ConnectionStrings:testDB"))).Returns(mockConfSection.Object);
收件人:
mockConfiguration.Setup(a => a.GetSection(It.Is<string>(s => s == "ConnectionStrings"))).Returns(mockConfSection.Object);
下一步设置打印“模拟值”:
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"
研究: Mocking IConfiguration from .NET Core
我需要对我的数据访问层进行集成测试,以确保所有代码都能正常工作。
我知道用正常的方式是行不通的:
//Will return a NotSupportedException
var mock = new Mock<IConfiguration>();
mock.Setup(arg => arg.GetConnectionString(It.IsAny<string>()))
.Returns("testDatabase");
通常数据访问层使用依赖注入,它使用 IConfiguration
检索连接字符串。
我的集成测试:
[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);
}
但是我得到一个错误
System.InvalidOperationException: The ConnectionString property has not been initialized.
我有点迷路了,我不确定发生了什么。
尝试更改:
mockConfiguration.Setup(a => a.GetSection(It.Is<string>(s => s == "ConnectionStrings:testDB"))).Returns(mockConfSection.Object);
收件人:
mockConfiguration.Setup(a => a.GetSection(It.Is<string>(s => s == "ConnectionStrings"))).Returns(mockConfSection.Object);
下一步设置打印“模拟值”:
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"