如何使用NHibernate有效地进行TDD?

时间:2022-12-02 21:25:22

It seems to me that most people write their tests against in-memory, in-process databases like SQLite when working with NHibernate. I have this up and running but my first test (that uses NHibernate) always takes between 3-4 seconds to execute. The next test runs much faster.

在我看来,大多数人在使用NHibernate时会针对像SQLite这样的内存中进程数据库编写测试。我已经启动并运行了,但我的第一个测试(使用NHibernate)总是需要3-4秒才能执行。下一个测试运行得更快。

I am using FluentNhibernate to do the mapping but get roughly the same timings with XML mapping files. For me the 3-4 second delay seriously disrupts my flow.

我使用FluentNhibernate进行映射,但使用XML映射文件获得大致相同的时序。对我来说,3-4秒的延迟严重扰乱了我的流量。

What is the recomended way of working with TDD and NHibernate?

推荐使用TDD和NHibernate的方式是什么?

Is it possible to mock ISession to unit test the actual queries or can this only be done with in memory databases?

是否可以模拟ISession对实际查询进行单元测试,或者只能在内存数据库中完成?

4 个解决方案

#1


14  

I am using the Repository Pattern to perform Database operations, and whenever I run my Tests I just run the higher-level tests that simply Mock the Repository (with RhinoMocks).

我正在使用存储库模式来执行数据库操作,每当我运行测试时,我只运行更高级别的测试,只需模拟存储库(使用RhinoMocks)。

I have a seperate suite of tests that explicitly tests the Repository layer and the NHibernate mappings. And those usually don't change as much as the business and gui logic above them.

我有一套独立的测试,可以显式测试Repository层和NHibernate映射。而那些通常不会像业务和gui逻辑那样改变它们。

That way I get very fast UnitTests that never hit the DB, and still a well tested DB Layer

这样我就可以获得非常快速的单元测试,而这些单元测试从未打到数据库,而且仍然是经过良好测试

#2


4  

Unit testing data access is not possible, but you can integration test it. I create integration test for my data access in a seperate project from my unit tests. I only run the (slow) integration tests when I change something in the repositories, mapping or database schema. Because the integration tests are not mixed with the unit tests, I can still run the unit tests about 100 times a day without getting annoyed.

单元测试数据访问是不可能的,但您可以集成测试它。我通过单元测试在单独的项目中为我的数据访问创建集成测试。当我在存储库,映射或数据库模式中更改某些内容时,我只运行(慢)集成测试。因为集成测试没有与单元测试混合在一起,所以我仍然可以每天运行100次单元测试而不会生气。

#3


1  

See http://www.autumnofagile.net and http://www.summerofnhibernate.com

请访问http://www.autumnofagile.net和http://www.summerofnhibernate.com

#4


0  

Have you tried changing some of the defaults in the optional configuration properties? The slowdown is most likely related to certain optimizations nhibernate does with code generation.

您是否尝试更改可选配置属性中的某些默认值?减速很可能与nhibernate对代码生成的某些优化有关。

http://nhibernate.info/doc/nh/en/index.html#configuration-optional

It seems like an in memory db is going to be the fastest way to test a your data layer. It also seems once you start testing your data layer you're moving a little beyond the realm of a unit test.

内存db似乎是测试数据层的最快方法。一旦你开始测试你的数据层,你似乎已经超出了单元测试的范围。

#1


14  

I am using the Repository Pattern to perform Database operations, and whenever I run my Tests I just run the higher-level tests that simply Mock the Repository (with RhinoMocks).

我正在使用存储库模式来执行数据库操作,每当我运行测试时,我只运行更高级别的测试,只需模拟存储库(使用RhinoMocks)。

I have a seperate suite of tests that explicitly tests the Repository layer and the NHibernate mappings. And those usually don't change as much as the business and gui logic above them.

我有一套独立的测试,可以显式测试Repository层和NHibernate映射。而那些通常不会像业务和gui逻辑那样改变它们。

That way I get very fast UnitTests that never hit the DB, and still a well tested DB Layer

这样我就可以获得非常快速的单元测试,而这些单元测试从未打到数据库,而且仍然是经过良好测试

#2


4  

Unit testing data access is not possible, but you can integration test it. I create integration test for my data access in a seperate project from my unit tests. I only run the (slow) integration tests when I change something in the repositories, mapping or database schema. Because the integration tests are not mixed with the unit tests, I can still run the unit tests about 100 times a day without getting annoyed.

单元测试数据访问是不可能的,但您可以集成测试它。我通过单元测试在单独的项目中为我的数据访问创建集成测试。当我在存储库,映射或数据库模式中更改某些内容时,我只运行(慢)集成测试。因为集成测试没有与单元测试混合在一起,所以我仍然可以每天运行100次单元测试而不会生气。

#3


1  

See http://www.autumnofagile.net and http://www.summerofnhibernate.com

请访问http://www.autumnofagile.net和http://www.summerofnhibernate.com

#4


0  

Have you tried changing some of the defaults in the optional configuration properties? The slowdown is most likely related to certain optimizations nhibernate does with code generation.

您是否尝试更改可选配置属性中的某些默认值?减速很可能与nhibernate对代码生成的某些优化有关。

http://nhibernate.info/doc/nh/en/index.html#configuration-optional

It seems like an in memory db is going to be the fastest way to test a your data layer. It also seems once you start testing your data layer you're moving a little beyond the realm of a unit test.

内存db似乎是测试数据层的最快方法。一旦你开始测试你的数据层,你似乎已经超出了单元测试的范围。