TDD - 100%覆盖率的方法来说包裹SQL [英] TDD - 100% Coverage for methods that wrap SQL

查看:115
本文介绍了TDD - 100%覆盖率的方法来说包裹SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚读完用C#专业测试驱动开发,并一直试图找到一种方法,在我的代码,实现100%覆盖。这一切都很好,直到我打了一个充满实现这样的方法仓储类:



<预类=郎-CS prettyprint-覆盖> 公共IEnumerable的< MyDataContract> LoadConditional(布尔isCondition)
{
常量字符串QUERY = @SELECT ... ...场迈德特FROM WHERE [IsCondition] = @IsCondition;
返回DataAccessor.ReadMyContracts(QUERY,isCondition); //什么,什么......
}



我一直在想这个对一段时间,一直没能找到直接回答这个问题在互联网上一个答案。




  • 我读到的东西,会建议SQL相关业务将存在另一个程序。我不要求这虽然不相信我应该去那里。而这一点,从代码覆盖的角度来看是一个非常肤浅的改变。


  • 我读过,你可以连接数据库的单元测试(我'已经做过)。但是,这只是嗯......我不知道,这感觉不对。该测试是缓慢的,在维护显著上升。




我的直觉是,如果没有最后一点我提到,这种方法不能单元测试。我应该怎么看这个问题?


解决方案

首先我要说,我认为,要实现100%的覆盖是毫无意义可言,并不能证明任何事情。



话虽这么说,我通常使用DB和业务逻辑之间的一些层 - 一些简单的映射器(PetaPoco,短小精悍,OrmLite),或很少,一个完全成熟的ORM(NHibernate的)。



在我需要对一个DB集成测试的情况下,这些工具允许我对运行测试DB相同的查询(如一个内存中的SQLite DB),而不是真实的数据库服务器。



至于你的担忧,测试是缓慢的,在维护显著上升。你应该记住,这些都不是单元测试了 - 这些都是集成测试,他们不能以最快的速度单元测试


I've just finished reading "Professional Test-Driven Development with C#" and have been trying to find a way to achieve 100% coverage in my code. It's all good until I hit a repository class that is filled with methods implemented something like this:

public IEnumerable<MyDataContract> LoadConditional(bool isCondition)
{
   const string QUERY = @"SELECT ...fields... FROM MyData WHERE [IsCondition] = @IsCondition";
   return DataAccessor.ReadMyContracts(QUERY, isCondition); // something, something...
}        

I've been thinking about this for some time, and have not been able to find an answer on the internet that answers this question directly.

  • I read things that would suggest that SQL related business would exist in another assembly. I don't require this though and don't believe I should have to go there. And this, from a code coverage, perspective is a pretty superficial change.

  • I've read that you can hook up databases for your unit tests (which I've done before). But this just well... I dunno, it doesn't feel right. The tests are slow and have a significant increase in maintenance.

My gut feeling is that without the last bit I mentioned, this method can't be unit tested. How should I be viewing this problem?

解决方案

First let me say that I believe that achieving 100% coverage makes no sense at all and doesn't prove anything.

That being said, I typically use some layer between DB and business logic - some simple mapper (PetaPoco, Dapper, OrmLite) or, rarely, a full blown ORM (NHibernate).

In cases where I need integration tests against a DB, these tools allow me to run the same queries against a test DB (e.g. an in-memory SQLite DB) instead of 'real' DB server.

With regard to your concern that "the tests are slow and have a significant increase in maintenance." you should bear in mind that these are not unit tests anymore - these are integration tests and they can't be as fast as unit tests.

这篇关于TDD - 100%覆盖率的方法来说包裹SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆