简体   繁体   English

数据测试层的单元测试

[英]Unit Testing a Data Access Layer

I know the question has been asked many many times before, but still I haven't found a good answer, and my take on the issue is slightly different. 我知道这个问题已经被问过很多次了,但是我仍然没有找到一个好的答案,我对此问题的看法略有不同。

I'm looking for a good way to unit test a Data Access Layer, thoroughly, but if possible atomically. 我正在寻找一种彻底测试数据访问层的好方法,但是如果可能的话,可以进行原子测试。 I don't want to use the real Database (or a clone) because it would be an integration test, and I want my tests to remain as lightweight and simple as possible. 我不想使用真实的数据库(或克隆),因为这将是一个集成测试,并且我希望我的测试保持尽可能轻巧和简单。

The DAL is implemented using NHibernate, and the DB is Microsoft SQL Server. DAL是使用NHibernate实现的,而DB是Microsoft SQL Server。 unfortunately some of the DAOs will have to be implemented using plain old ADO.Net. 不幸的是,某些DAO必须使用普通的ADO.Net来实现。 And even worse some of the DAO's would have to be a wrapper around Stored Procedures. 甚至更糟的是,某些DAO必须是存储过程的包装。

Basically, what I want to test, is that the NHibernate mappings make sense, and that the DAO fundamentally works. 基本上,我要测试的是NHibernate映射有意义,并且DAO从根本上起作用。 I want to create a DB in memory based on my NHibernate mappings, Mock all the required Stored Procedures, and then run my unit test's around this database. 我想根据我的NHibernate映射在内存中创建一个数据库,模拟所有必需的存储过程,然后围绕该数据库运行我的单元测试。 Inserting and querying using the DAO I'm testing, from the 'Mock' in memory database. 使用我正在测试的DAO从内存数据库中的“模拟”插入和查询。

My questions are as follows: 我的问题如下:

  1. Is this a good approach? 这是一个好方法吗?
  2. does anybody have experience with this approach and can share insights? 是否有人有这种方法的经验并可以分享见解?
  3. Is there a some framework or a library that can help me with creating the mock in memory database? 是否有一些框架或库可以帮助我创建内存中的模拟数据库?
  4. How can I build these tests to work for an NHibernate based Dao's and ADO.Net based Dao's? 如何建立这些测试以用于基于NHibernate的Dao和基于ADO.Net的Dao?
  5. How can I create mocks for stored procedures? 如何为存储过程创建模拟?

Testing around the DAO is always tricky, but if you have logic inside this layer then it is definitely a good candidate for unit testing. 围绕DAO进行测试总是很棘手,但是如果您在这一层中有逻辑,那么它绝对是单元测试的理想选择。

I have found in the past that using an in-memory database for this type of testing works very well - there is a certain amount of setup cost involved (ie to configure the connection and deployment of your own DB schema as part of the tests) but once this is done they tend to work quite nicely. 过去我发现使用内存数据库进行这种类型的测试非常有效-涉及一定数量的设置成本(即,在测试中配置您自己的数据库模式的连接和部署)但是一旦完成,他们往往会表现得很好。 SQLite looks like it could be a good option for you, it looks like it is fairly straightforward to use with NHibernate , and there is also an SQLite provider for ADO.net . SQLite看起来可能是一个不错的选择,看起来与NHibernate一起使用非常简单,并且还有一个ADO.netSQLite提供程序

One issue with SQLite for your problem is that it doesn't support stored procedures. 对于您的问题, SQLite一个问题是它不支持存储过程。 I think that your best bet here is to create separate classes that encapsulate the stored procedure calls, and dependency inject objects of these types into your DAO's. 我认为,这里最好的选择是创建单独的类来封装存储过程调用,并将这些类型的对象依赖注入到DAO中。 That way, you can use standard object mocking to mock out the stored procedure calls. 这样,您可以使用标准对象模拟来模拟存储过程调用。

One final note - if you do have stored procedures in your application that contain any non-trivial logic, then putting some integration tests around these could prove worthwhile (perhaps one test per stored proc that exercises the DAO and the proc deployed in a production like database). 最后一点-如果您的应用程序中确实有包含任何非平凡逻辑的存储过程,那么围绕它们进行一些集成测试可能是值得的(也许每个存储proc进行一个测试,该测试将对DAO和部署在生产环境中的proc进行练习)数据库)。 Although these will be somewhat painful to create and maintain, I have found these to be very worthwhile in the past - changes to stored procedures were often the source of bugs due to the fact developers rarely tested stored proc changes thoroughly enough. 尽管创建和维护它们有些痛苦,但我发现它们在过去非常值得-由于开发人员很少充分测试存储proc更改,因此对存储过程的更改通常是bug的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM