【发布时间】:2021-01-05 14:20:19
【问题描述】:
我有一个与员工合作的应用程序。所以我决定使用 Mock 来测试 Employee Repository。
这是我的测试方法
class EmployeesRepository : IEmployeeRepository ...
public IEnumerable<Employee> GetAllEmployees()
{
List<Employee> list = new List<Employee>();
try
{
string connectionString = _secureConfig.Value.MyDbSetting;
string sql = "select id, firstname, lastname, entrydate, email from empoyees";
using SqlConnection connection = new SqlConnection(connectionString);
using SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
using SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
list.Add(new Employee
{
Id = reader.GetString(0),
FirstName = reader.GetString(1),
LastName = reader.GetString(2),
EntryDate = reader.GetDateTime(3),
Email = (reader.IsDBNull(4) ? null : reader.GetString(4))
});
}
}
catch (Exception ex)
{
_log.LogError(ex, "An error is caught when getting all employees");
}
return list;
}
我的问题是在这种情况下模拟什么以及如何模拟......我应该模拟数据读取器,还是只模拟 ExecutReader 方法......请提供建议,从哪里开始测试这些方法并直接访问数据库。
【问题讨论】:
标签: .net .net-core integration-testing moq .net-core-3.1