开发者

Applying CQRS - Is unit testing the thin read layer necessary?

开发者 https://www.devze.com 2023-02-08 04:41 出处:网络
Given that some of the advice for implementing CQRS advocates fairly close-to-the-metal query implementation, such as ADO.NET queries directly against the database (or perhaps a LINQ-based ORM), is it

Given that some of the advice for implementing CQRS advocates fairly close-to-the-metal query implementation, such as ADO.NET queries directly against the database (or perhaps a LINQ-based ORM), is it a mistake to try and unit test them?

I wonder if it's really even necessary?

My thoughts on the matter:

  1. The additional architectural complexity to provide a mockable "Thin Read Layer" seems opposite to the very nature of the advice to keep the architectural ceremony to a minimum.
  2. The number of unit tests to effectively cover every angle of query that a user might compose is horrendous.

Specifically I'm trying CQRS out in an ASP.NET MVC application and am wondering whether to bother u开发者_运维知识库nit testing my controller action methods, or just test the Domain Model instead.

Many thanks in advance.


In my experience 90%-99% of the reads you will be doing if you are creating a nice de-normalized read model DO NOT warrant having unit tests around them.

I have found that the most effective and efficient way to TDD a CQRS application is to write integration tests that push commands into your domain, then use the Queries to get the data back out of the DB for your assertions.


I would tend to agree with you that unit testing this kind of code is not so beneficial. But there is still some scope for some useful tests.

You must be performing some validation of the user's read query parameters, if so, then test that invalid request parameters throw a suitable exception, and valid parameters are allowed.

If you're using an ORM, I find the cost/benefit ratio too great for testing the mapping code. Assume your ORM is already tested, there could be errors in the mapping, but you'll soon find and fix them.

I also find it useful to write some Integration tests (using the same Testing framework), just to be sure I can make a connection to the database, and the ORM configuration doesn't throw any mapping exceptions. You certainly don't want to be writing unit tests that query the actual db.


As you probably already know unit testing is less about code coverage and preventing bugs than it is about good design. While I often skip testing the read-model event handlers when I'm in a hurry, there can be no doubt that it probably should be done for all the reasons code should be TDD'd.

I also have not be unit testing my HTTP actions (I don't have controllers per se since I'm using Nancy not .NET MVC).

These are integration points and don't tend to contain much logic since most of it is encapsulated in the command handlers and domain model.

The reason I think it is fairly easy not to test these is because they are very simple and very repetitive, there is almost no deep thinking in the denormalization of events to the read-model. The same is true for my HTTP handlers, which pretty much just process the request and issue a command to the domain, with some basic logic for return a response to the client.

When developing, I often make mistakes in this code and I probably would make far fewer of these mistakes if I was using TDD, but it would also take much longer and these mistakes tend to be very easy to spot and fix.

My gut tells me I should still apply TDD here though, it is still all very loosely coupled and it shouldn't be hard to write the tests. If you are finding it hard to write the tests that could indicate a code smell in your controllers.


The way that I have seen something like this unit tested is to have the unit test create a set of things in the database, you run your unit tests, then clean out the created things.

In one past job I saw this set up very nicely using a data structure to describe the objects and their relationships. This was run through the ORM to create those objects, with those relationships, data from that was used for the queries, and then the ORM was used to delete the objects. To make unit tests easier to set up every class specified default values to use in unit tests that didn't override those values. Then the data structure in the unit tests only needed to specify non-default values, which made the setup of the unit tests much more compact.

These unit tests were very useful, and caught a number of bugs in the database interaction.


In one my asp.net mvc application i've also applied sqrc. But instead of sql and 'ADO.NET queries' or 'Linq' we using document database(mongodb) and each command or event handler direct update database.

And i've created one test for one command/event handler. And after 100% unit testing i know that my domain work on 95% correct. But for actions/controllers/ui i've applied ui tests(using selenium).

So seems both unit tests for the domain(commands/events handlers and direct updates to database) and ui tests it your 'integration tests'.

I think that you should test domain part at least, because all your logic incapsulated in command/event handlers.

FYI: I've also started developing domain part first from entity framework, than direct updates into database through stored procedures but was really happy with document database. I tried some different document databases but mongodb looks like best for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消