In the past, when I implemented unit test, I struggled to set up “decent” unit tests for data access levels, as they often have a database as an external dependency. In an ideal world, I ridiculed calls to a stored procedure so that the external dependency is removed.
However, I was not able to figure out how to do this with falsification of MOQ or to find any other infrastructure that supports this. Instead, I returned to creating a test test database with known data (so that I can always get the output that I expect), but this is slightly different from Mocking at this level.
Can anyone suggest how to debug this part of the data access layer [I know for the Entity Framework that https://effort.codeplex.com/ exists ??
<B> Detail For example, if I have the following method
public object RunStoredProc() { //Some Setup using (SqlConnection conn = new SqlConnection(CONNNECTION_STRING)) { using (SqlCommand comm = new SqlCommand("storedProcName", conn)) { conn.Open(); comm.CommandType = CommandType.StoredProcedure; using (SqlDataReader reader = comm.ExecuteReader()) { while (reader.Read()) { //Logic } } } } //Return object based on logic }
then how can I make fun of the output of a stored procedure so that SQLDataReader contains the specified data. At a higher level, I could make fun of the RunStoredProc() method, but that will not help me verify the logic in this method. Alternatively, I could remove SQLReader in another method
public object RunStoredProc() { //Some Setup List<object> data = GetData(); //Logic //Return object based on logic } private List<object> GetData() { using (SqlConnection conn = new SqlConnection(CONNNECTION_STRING)) { using (SqlCommand comm = new SqlCommand("storedProcName", conn)) { conn.Open(); comm.CommandType = CommandType.StoredProcedure; using (SqlDataReader reader = comm.ExecuteReader()) { while (reader.Read()) { //place into return object } } } } }
but since the GetData method must be private (not part of the published interface), then I could not mock it, and therefore the problem remains.