I have an application working with Employees. So I decided to use Mock to test the Employee Repository.
Here is my method to test
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;
}
My question is what and how to mock in this case... should I mock the datareader, or just only the ExecutReader method... please give an advice from where to start the test of such methods with direct access to the DB.