如何测试我的存储库实施?
我使用 NUnit 作为测试单元。我在域上有我的接口,因此我准备在持久层中实现这些接口。我的问题是,您实际上如何进行单元测试来测试这些存储库?我认为直接从数据库进行测试并不是一个好主意。我听说有人正在使用 SQLite,但使用模拟也可以吗?当你可以提供带有实际实体的模拟时,为什么人们使用 SQLite 作为内存数据库?
任何例子也将受到欢迎。
注意:这是用 C# 编码的存储库,将使用 NHibernate 和 Fluent NHibernate 作为映射。
谢谢。
I am using NUnit for test units. I have my interface on the domain so i am ready to make implementation of those interfaces in the persistence layer. My question is how do you actually make the unit tests for testing those repositories ? i believe this isnt a good idea to test directly from the database. Ive heard poeple that are using SQLite but it is okay to use mocks instead ? why are the poeple using SQLite for in-memory database when you can provide a mock with actuals entities ?
Any example would be welcome too.
Note: This is intended to be repositories coded in C# that gonna use NHibernate and Fluent NHibernate as mapping.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这当然取决于,但在大多数情况下,我想说,通常只需在测试中模拟存储库并使用内存中的 SQLite 数据库来测试您的映射就足够了(FluentNHibernate 持久性规范测试)。
对于使用 SQLite 的 NUnit 映射测试,我使用以下基类:
从上述基类派生的示例映射测试类可能如下所示:
It of course depends, but in most cases I'd say it's generally enough to just mock the repositories in your tests and using the in-memory SQLite database only to test your mappings (FluentNHibernate Persistence specification testing).
For the NUnit mappings tests with SQLite I'm using the following base class:
An example mappings test class deriving from the above base class could then look like the following:
就我个人而言,我会针对实际数据库(可能是 SQL Express)对存储库进行功能测试。您每天只能在 CI 中运行一次这些测试。
其他类的所有单元测试都可以安全地假设存储库正常工作并使用模拟存储库。
编辑:以上假设您的存储库仅用于数据访问;他们基本上只使用 LINQ 或 HQL。让他们远离业务逻辑!
Personally I'd do functional testing of the repositories against an actual database (possibly SQL Express). You could run those tests in CI only once a day.
All the unit tests for other classes can safely assume that the repositories work and use mock repositories.
EDIT: The above presumes that your repositories are solely used for data access; they basically just use LINQ or HQL. Keep the business logic out of them!