使用生成的 DAL 代码进行单元测试
我使用代码生成器(带有 .NetTiers 模板的 CodeSmith)来生成所有 DAL 代码。 我为我的代码(业务层)编写单元测试,这些测试运行起来变得非常慢。 问题是,对于每个测试,我都会将数据库重置为干净状态。 另外,当我进行大量测试时,似乎数据库操作的延迟总计相当大。
所有数据库操作均通过 .NetTiers 生成的 DataRepository 类执行。 您是否知道是否有一种方法可以生成(或自己编码)一个模拟 DataRepository ,它会使用内存存储而不是使用数据库?
这样,我就可以在单元测试中使用这个模拟存储库,从而大大加快测试速度,而无需对当前代码进行任何实际更改!
I use a code generator (CodeSmith with .NetTiers template) to generate all the DAL code. I write unit tests for my code (business layer), and these tests are becoming pretty slow to run. The problem is that for each test, I reset the database to have a clean state. Also, as I do a lot of tests, it seems that the latency of the database operations sum up to a pretty bit delay.
All DB operations are performed through a DataRepository
class that is generated by .NetTiers. Do you know if there is a way to generate (or code myself) a mock-DataRepository that would use in-memory storage instead of using the database ?
This way, I would be able to use this mock repository in my unit tests, speeding them a lot, without actually changing anything to my current code !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下依赖注入 (DI) 和控制反转容器 (IOC)。 本质上,您将创建一个新的模拟数据库对象可以实现的接口,然后 DI 框架将在运行测试时注入您的模拟数据库,并在运行您的应用程序时注入真实的数据库。
有许多免费和开源库可以用来帮助您。 由于您使用的是 C#,因此新兴的 DI 库之一是 Ninject。 还有很多其他的。 查看这篇维基百科文章了解其他内容和高级描述。
Take a look at Dependency injection (DI) and Inversion of Control containers (IOC). Essentially, you will create an interface that that a new mock DB object can implement, and then the DI framework will inject your mock DB when running tests, and the real DB when running you app.
There are numerous free and open source libraries that you could use to help you out. Since you are in C#, one of the new and up and coming DI libraries is Ninject. There are many others too. Check out this Wikipedia article for others and a high level description.
从问题的描述来看,我认为您正在执行集成测试,因为您的测试正在使用业务、DAL 和实时数据库。
对于单元测试,您需要处理一层代码,并模拟或存根所有其他依赖项。 通过这种方法,您的单元测试将非常快速地对每个增量代码更改执行。
您可以使用各种模拟框架,例如 Rhino Mock、Moq、typemock 等。 (在我的项目中,我使用Rhino模拟来模拟DAL层并隔离单元测试业务层)
Harsha
From the description of the issue, I think you are performing the Integration test because your test is making use of the Business and the DAL and live database.
For unit testing, you deal with one layer of code with all other dependencies either mocked or stubbed. With this approach, you unit tests will be really fast to execute on every incremental code changes.
There are various mocking frameworks that you can use like Rhino Mock, Moq, typemock to name a few. (In my project, I use Rhino mock to mock the DAL layer and unit test Business Layer in Isolation)
Harsha
我们的一些单元测试使用从数据库生成的 XML 中获取的数据来模拟数据库访问。 DAL 类被模拟类取代,因为它们都存储在 DI 容器中。
xml 的生成是自定义代码,如果您找到一个开源解决方案,那么我很高兴听到它。
在 Stefan 的回答后编辑:我记得另一个团队使用 SQL CE 作为他们的测试数据库
Some of our unit tests use data fetched from XML's which were generated from a database to mock db access. DAL classes are replaced by mock ones because they are all stored in a DI container.
The generation of the xml's is custom code, if you find an open source solution for this then I'm happy to hear it.
Edit after Stefan's answer: I recall another team using SQL CE for their test database