Unit testing, a project with many database calls

I have a question about unit testing. I currently have a large project that calls a lot of SP and does not get a return for most methods. This is actually a great wrapper for many SQL calls. There is not much logic, since they are all contained in SP, it also has sections in the sql line.

I need unit test this C # project, but it becomes clear that unit test will be pointless, as it will cause a lot of SPs that all will mock. I worry, I think about it wrong.

My question is, did anyone have this problem and what did they do? If I were doing database module tests, any insight would be a big help.

Thanks.

+4
source share
4 answers

A unit test should not concern the level of data access, as it will be an integration test / system test. What you can check is that your project actually calls up your level of data access. Doing this will give you peace of mind that during refactors that clicking a button always causes a level of data access.

//Arrange var dataAccessMock = new Mock<IDataAccessMock>(); dataAccessMock(da => da.ExecuteSomething()); IYourApplication app = new YourApplication(dataAccessMock); //Act app.SomeProcessThatCallsExecuteSomething("1234567890"); /Assert dataAccessMock.Verify(dp=>da.ExecuteSomething(), Times.Once()); 

Notice in this example I use Moq

Once this is verified to your liking, you can focus on your integration test to verify that your stored procedures work as intended. To do this, you may need to do a little work to attach the database in a known state, start your stored procedures, and then return or destroy the database so that the tests are repeated.

0
source

You must separate the testing strategy into integration testing and unit testing.

You can rely on an existing database to test integration. Usually you write higher-level tests and make sure your application interacts correctly with your database.

For unit testing, you should select only selected scenarios that really make sense for bullying. These are typically scenarios in which a lot of business logic sits on top of your database logic, and you want to test this business logic. Over time, you can make fun of more and more database queries, but first, identify critical points.

0
source

You have discovered one reason why business logic should usually go to a business rather than to access data at a level. Of course, there are exceptions due to performance and sometimes security considerations, but they should remain exceptions.

Having said that, you can still develop a testing strategy for your sprocs (although depending on how extensive they are, it may or may not be right to call these tests "unit tests").

You can use the unit testing platform anyway.

In the initialization section, restore the test copy of the database to a known state, for example. downloading it from a previously saved copy.

Then run the unit tests that execute the stored procedures. Because stored procedures usually do not return anything, your unit test code will need to select values ​​from the database to check if the expected changes were made or not.

Depending on the possible interactions between stored procedures, you may need to restore the database between each test or between groups of related tests.

0
source

The data / containment level can often be the most forgotten code in terms of unit testing (true unit testing using test doubles: layouts, stubs, fakes, etc.). If you connect to the database, you perform integration testing. I find value in a) well-structured data / persistence layers that, as a side effect, are easily tested (using interfaces, abstracting the underlying data structure, etc. And b) are actually a proven property of unit and integration.

0
source

All Articles