単体テストとモッキングの方法を学ぼうとしています。TDD と基本的なテストの原則の一部を理解しています。ただし、テストなしで記述された以下のコードのリファクタリングを検討しており、テスト可能にするためにどのように変更する必要があるかを理解しようとしています。
public class AgentRepository
{
public Agent Select(int agentId)
{
Agent tmp = null;
using (IDataReader agentInformation = GetAgentFromDatabase(agentId))
{
if (agentInformation.Read())
{
tmp = new Agent();
tmp.AgentId = int.Parse(agentInformation["AgentId"].ToString());
tmp.FirstName = agentInformation["FirstName"].ToString();
tmp.LastName = agentInformation["LastName"].ToString();
tmp.Address1 = agentInformation["Address1"].ToString();
tmp.Address2 = agentInformation["Address2"].ToString();
tmp.City = agentInformation["City"].ToString();
tmp.State = agentInformation["State"].ToString();
tmp.PostalCode = agentInformation["PostalCode"].ToString();
tmp.PhoneNumber = agentInformation["PhoneNumber"].ToString();
}
}
return tmp;
}
private IDataReader GetAgentFromDatabase(int agentId)
{
SqlCommand cmd = new SqlCommand("SelectAgentById");
cmd.CommandType = CommandType.StoredProcedure;
SqlDatabase sqlDb = new SqlDatabase("MyConnectionString");
sqlDb.AddInParameter(cmd, "AgentId", DbType.Int32, agentId);
return sqlDb.ExecuteReader(cmd);
}
}
これら 2 つのメソッドは 1 つのクラスにあります。GetAgentFromDatabase のデータベース関連のコードは、エンタープライズ ライブラリに関連しています。
これをテスト可能にするにはどうすればよいですか?GetAgentFromDatabase メソッドを別のクラスに抽象化する必要がありますか? GetAgentFromDatabase は IDataReader 以外のものを返す必要がありますか? 外部リンクへの提案やポインタは大歓迎です。