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;
}
Data access classes are all about integrating with DB. Roy Osherove for example consider this classes an exception when unit testing. I'm interested too on how to test this type of code. +1 for question.
IOW I'm not sure that it's advisable to mock around data access classes.
What not to test when it comes to Unit Testing?