私は単体テストに比較的慣れておらず、モッキングにもまったく慣れていません。データベースに接続せずに単体テストを作成したい DbProvider ファクトリをラップするデータベース クラスがあります。
クラスをテストするために渡すことができるように、DbProvider ファクトリをモックするにはどうすればよいでしょうか。DbConnection、DbCommand などもモックする必要がありますか? 私のコードの小さなサンプルは次のとおりです。
public Database(DbProviderFactory dbProviderFactory) {
Provider = dbProviderFactory;
}
public int UpdateRecords(string sql, CommandType type, params DbParameter[] parameters) {
int numberOfRecordsUpdated;
using (var connection = CreateConnection()) {
// Add ConnectionString
connection.ConnectionString = ConnectionString;
// Create command to hold the update statment
using (var command = CreateCommand()) {
try {
command.Connection = connection;
command.CommandType = type;
command.CommandText = sql;
// Add Parameters
foreach (var parameter in parameters) {
command.Parameters.Add(parameter);
}
// Open Connection
connection.Open();
// Execute SQL
numberOfRecordsUpdated = command.ExecuteNonQuery();
} finally {
command.Parameters.Clear();
}
}
}
return numberOfRecordsUpdated;
}