I am relatively new to unit testing, and completely new to mocking. I have a database class that wraps the DbProvider factory that I would like to create unit tests for without connecting to the database.
How would I mock the DbProvider factory, so I could pass it in to test my class? Would I also need to mock the DbConnection, DbCommand, etc.? A small sample of my code follows:
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;
}