问题描述
由于有些建议实施CQRS的主张相当接近到了金属的查询实现,如ADO.NET查询直接对数据库(或者一个LINQ为基础的ORM),它是一个错误的尝试和单元测试呢?
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?
我对此事的看法:
- 附加架构复杂以提供mockable薄读取层似乎相反的意见的性质,以保持建筑仪式到最小。
- 在单元测试的数量,有效地覆盖查询,一个用户可能构成每一个角度是可怕的。
具体我想CQRS在ASP.NET MVC应用程序,并想知道是否打扰单元测试我控制器操作方法,或者只是测试领域模型代替。
Specifically I'm trying CQRS out in an ASP.NET MVC application and am wondering whether to bother unit testing my controller action methods, or just test the Domain Model instead.
在预先感谢。
推荐答案
我倾向于同意你的说法,单元测试这类code不是那么有利。但仍有一定范围内对一些有用的测试。
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.
如果您使用的是ORM,我觉得成本/效益比太大,测试映射code。假设你的ORM已经测试过,有可能是在映射错误,但你很快就会发现并解决这些问题。
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.
我也觉得它有用编写一些集成测试(使用相同的测试框架),只是要确定我可以对数据库的连接,和ORM配置不抛出任何映射异常。你当然不希望编写查询实际分贝单元测试。
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.
这篇关于应用CQRS - 是单元测试薄读出层有必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!