Differences between database / entity and memory lists when taunted in unit tests

I have been doing a lot of mock unit testing lately. The only thing that bothers me is the differences between querying a list from memory (via the layout of my repository) and directly accessing the database through an entity.

Some of these situations may include:

  • Testing a filter parameter that is case-insensitive but case-sensitive against a memory collection leading to a false failure.

  • Linq statements that may be passed to memory collections but will not work with the entity infrastructure, as they arent supported, leading to a false pass.

What is the correct way to handle or account for these differences so that the tests do not have false passes or failures? I really enjoy mocking, as it makes things a lot faster and easier to test. But it seems to me that the only way to get a really accurate test is to simply test the entity / database environment.

+4
source share
4 answers

In addition to unit tests, you must also create integration tests that run with real database tuning, as was the case during production.

I am not an expert for EF, but with NHibernate, for example, you can create a configuration that points to an instance of SQLite in memory, where you then run quick tests (i.e. during the development cycle, where you want to get through the test suite as much as possible faster). When you want to run your integration tests with a real database, you simply modify the NHibernate configuration to indicate the actual database setup and repeat the same tests again.

It would be surprising if you could not achieve something similar with EF.

+2
source

You can use DevMagicFake , this framework will fake the database for you and can also generate data so you can test your application without testing DB

0
source

First of all, you can define any behavior data in your layout. The second is speed. From a single test, the speed is checked in the future. Database connections are a bottleneck in most cases, so you scoff at testing. To run the test correctly, you first need to work with your shared arch. For example, to access the data layer, I sometimes use the repository template. This is described very well in Eric Evans DDD book. So, let's say if your repository is defined below the IRepository interface: IQueryable, ICollection, you can handle linq queries quite simply. Further reading of the Repository

0
source

I would make my mocks more detailed so that you are not actually requesting a large set in the mock repository. Usually I have setters in my wet repository that I installed in each test to control the output of the mocking repository. Thus, you do not need to rely on recording requests for a common layout, and your focus may be on checking the logic in the test method

0
source

Source: https://habr.com/ru/post/1403175/


All Articles