我如何有效地与NHibernate做TDD? [英] How do I do TDD efficiently with NHibernate?

查看:170
本文介绍了我如何有效地与NHibernate做TDD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,大多数人在使用NHibernate时,像SQLite一样在内存中,进程内数据库上编写测试。我有这个和运行,但我的第一个测试(使用NHibernate)总是需要3-4秒执行。下一个测试的运行速度要快得多。

我使用FluentNhibernate来执行映射,但是与XML映射文件的计时大致相同。对我来说,3-4秒的延迟严重干扰了我的流程。

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



是否有可能嘲笑ISession单元测试实际的查询,或者只能在内存数据库中完成?

解决方案

我使用Repository模式来执行数据库操作,每当我运行我的测试时,我只是运行简单的模拟仓库(与RhinoMocks)的更高级别的测试。

我有一个独立的测试套件,明确地测试了Repository层和NHibernate映射。而那些通常不会像上面的业务和gui逻辑那样改变。

通过这种方式,我获得了非常快的UnitTests,从来没有碰到数据库,而且还是一个经过良好测试的数据库层


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.

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.

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

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

解决方案

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).

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.

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

这篇关于我如何有效地与NHibernate做TDD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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