Mocking DbProviderFactory

I'm relatively new to unit testing and completely new to ridicule. I have a database class that wraps a DbProvider factory, which I would like to create unit tests without connecting to the database.

How would I make fun of the DbProvider factory so that I can go through it to test my class? Should I also make fun of DbConnection, DbCommand, etc.? Below is a small example of my code:

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; } 
+4
source share
3 answers

Integration Testing


If you are interested (as judged by your comment) when testing the receipt of data from the database, you will perform the integration test .

The difference in unit testing for these types of tests is that they are performed less frequently - best practice dictates that you run them before committing the source code.

The reason is that they are slower and you will end up in a real database (at least in a test database). On each run, you will need to erase the database or not commit the changes - you seem to know what you are doing here, so I will leave how you deal with it yourself.

Mocking


As for testing your logic, which uses a database - mocking and dependency injection is the way to go.

I use Moq - its probably the easiest mocking structure to use. In fact, this is not only a mock of objects (despite the name), and you can create stubs and fakes with it.

Pseudo mocking goes like this (using the DB example):

  • Customize layout
  • Tell me what to do (expect) - for example. call to save.
  • Use SUT (system test)
  • Check the layout - in this case, the save method was called in your SUT.

For more help - check out Google - there are some good unit testing resources and ridicule.

+2
source

I personally mocked a class that contains higher-level data access functions (like UpdateRecords, etc.) and uses this layout to return predefined datasets.

Using DbProvider and corresponding DbCommand etc. is a detail of the implementation of your data access level and is not related to higher level functionality that uses return values.

+1
source

All data access classes are database integration. For example, Roy Osherow treats these classes as an exception in unit testing. I am also wondering how to check this type of code. +1 for the question.

IOW I'm not sure if it’s advisable to mock data access classes.

What should not be checked when it comes to Unit Testing?

0
source

All Articles