How do you unit test a class that is designed to communicate with data?

I have several repository classes that are designed to talk with various types of data flowing from the IRepository interface.

In implementations, the code speaks to the data source, whether it is a directory of XML files or a database, or even just a cache. Is it possible to reliably unit test run any of these implementations? I don’t see a feigned implementation, because then I only check the code layout, not the actual code.

+6
c # unit-testing tdd mstest
source share
4 answers

No, you would use a layout when writing a class that uses IRepository . For IRepository implementations IRepository you need to test the appropriate data source. For databases, this is a bit of a pain - for the file system, a little less.

If possible, if you can express your implementation in terms of threads or readers, you will make your life easier: tests for those parts of the implementation can go against data sources in memory or threads from resources in a test assembly. Of course, you probably need some tests that fall into a real database or file system, but hopefully less.

Whether you will call such tests a “unit” or not is a question of how you define unit tests; I personally don’t care too much about names, but I don’t care that I have tests. For databases, in particular, this can be somewhat painful (especially if you want to be able to run tests in parallel), but they can also be incredibly valuable, in my experience.

+8
source share

I think that if you are testing code that actually stores or requests data, you probably really want to get into the database.

These are integration tests, not unit tests.

You can set up a test database in which you know the state of the data and run tests against it. You probably also want to tell the tests that they are different from your unit tests, and you don’t need to run them every time you test (in nUnit, you can decorate your test class with an attribute saying that it does not start)

+1
source share

In general, you will not be unit test with any code whose sole purpose is to communicate with the data source. You can still automatically test the repository, but such a test will by definition be an integration test. You probably do not want to run these tests as part of your “first passes”, for example, for example. setting up the database and cleaning up after you yourself can take a not insignificant amount of time.

If your repository has other responsibilities (for example, implementing the Unit of Work template), you might want to unit test separately.

+1
source share

At some point in the implementation of IRepository, you will use a third-party API that will actually read / write to / from the database / file / xml. What you want to do is mock these APIs to make sure your code calls the correct API in the correct order.

So, if you are reading from a database, you can simulate SqlConnection and SqlCommand and make sure that you call the correct methods for these classes. If you are writing a stream, you can mock the stream and make sure that you clean and delete it (for example).

+1
source share

All Articles