In our environment, we are using a custom data access to access our legacy Oracle database. It allows us to make calls to stored procedures and get the results back as a DataTable or as a list of name/value pairs if the data returned is only one row of data. It requires us on each call to instantiate a new object from this library and then make sure it gets disposed of upon completion.
For example, we might do something like this:
DataReader reader = new DataReader("StoredProcedureName");
reader.AddParameter(name, DbType.Array, ParameterDirection.Input, data);
DataTable dt;
try
{
dt = reader.Open();
}
finally
{
reader.Dispose();
}
My problem is I am unsure of how to test methods that make database calls using this library. I am relatively new to unit testing and am trying to start using it diligently. I have gotten started using moq and have written a bunch of test cases for my other application code. I don’t know how I can mock the database library. Would it make sense to build a factory object that will create new instances of the datareader and then mock the factory?
Thanks in advance.
Your not going to be able to “unit” test the very lowest level of your code (i.e. thin wrapper around ADO.Net that you are calling your Data Access Layer). Every other layer you can use mocking to isolate it, but in order to test the very lowest layer you should just write “system” tests that will actualy hit a real database (this will involve setting it up so you can recreate the DB in a known state as part of the test run – Database Projects are great for this).