I’ve created an extension method for SqlCommand that allows some additional work before executing the command:
public static SqlDataReader ExecuteMyReader( this SqlCommand sqlCommand )
{
// Some calculations here
return sqlCommand.ExecuteReader();
}
My question is what is the best way to unit test this extension method?
Should I test it directly against a database?
Or should I try and create a mock object? If this is the case I’ve tried doing this through Moq but as SqlCommand is a sealed class I can’t seem to mock it.
Can you pull the calculations out to their own method and test them separately? As you’ve discovered, it’s difficult and unrewarding to write unit tests for functions at this level of granularity.