Testing AR

3 views
Skip to first unread message

uluho...@yandex.ru

unread,
Oct 5, 2008, 6:12:23 AM10/5/08
to Castle Project Users
Hi,

I'm going to try Active Record in my next project, and I'd like to get
some advice on testable design. Specifically, I'd like to avoid
talking to the database as much as possible.

For example, suppose I've got a ProductRepository class, and I want to
test the GetProductsByCategory method. The most straightforward way
would be to create a Category, a Product, save them to the database,
then just run the query. But that's a lot of extra work: you have to
ensure all the mandatory fields, you have to cleanup etc. Doesn't
sound like a unit test.

I don't know much about NHibernate, but I gathered that there's some
sort of in-memory cache. What if I could just put my Product into that
cache, not saving it, and run the subsequent query against this cache?
Is it possible?

On the other hand, I have seen that there's some in-memory database in
Rhino Commons, but the wiki link is broken, and I couldn't find it in
the source. Anybody uses it? Will it help me with that problem?

Thanks a lot for your help

ulu

Ken Egozi

unread,
Oct 5, 2008, 6:46:42 AM10/5/08
to castle-pro...@googlegroups.com
what people usually do for end-to-end  tests is to setup an in-memory DB like Sql-Lite, use NH (or AR) to generate the schema on fixture setup, then do the "put test data in the DB, query and assert" which should run fast-ly as it's in memory.

personally I try to have as less db-involving tests as possible, and not to run them on every build (only daily or pre-deploy), then I use my regular DB for these (- SQL Server).

I need not NH's ability to Load by PK, or simple queries/criterias.
I trust NH for testing NH.
So the controller/service tests would simply use faked repositories, while repository tests (which will involve the DB) will be kept to the complex scenarios - i.e when I need to make sure that a complex query actually returns what it's supposed to do.

uluho...@yandex.ru

unread,
Oct 5, 2008, 1:42:14 PM10/5/08
to Castle Project Users
That's what I was expecting..

Can I find the SQLite config settings somewhere?

thanks..

On 5 окт, 14:46, "Ken Egozi" <egoz...@gmail.com> wrote:
> what people usually do for end-to-end  tests is to setup an in-memory DB
> like Sql-Lite, use NH (or AR) to generate the schema on fixture setup, then
> do the "put test data in the DB, query and assert" which should run fast-ly
> as it's in memory.
>
> personally I try to have as less db-involving tests as possible, and not to
> run them on every build (only daily or pre-deploy), then I use my regular DB
> for these (- SQL Server).
>
> I need not NH's ability to Load by PK, or simple queries/criterias.
> I trust NH for testing NH.
> So the controller/service tests would simply use faked repositories, while
> repository tests (which will involve the DB) will be kept to the complex
> scenarios - i.e when I need to make sure that a complex query actually
> returns what it's supposed to do.
>
> On Sun, Oct 5, 2008 at 12:12 PM, uluhonol...@gmail.com <

Christian Wuerdig

unread,
Oct 5, 2008, 3:17:35 PM10/5/08
to castle-pro...@googlegroups.com
SQLite in memory is a little bit tricky because every time you close a
session usually the connection is closed and then your database is gone
Ayende has a blogged about that:
http://ayende.com/Blog/archive/2006/10/14/UnitTestingWithNHibernateActiveRecord.aspx

For me this doesn't work out of the box, because sometimes I have to
test a presenter, which uses sessions internally. But I found a good
workaround here:
http://notepad2.wordpress.com/2008/05/19/unit-testing-castle-active-record-using-sqlite-in-memory-database/

The configuration (using InPlaceConfigurationSource):
properties.Add("hibernate.connection.driver_class",
"NHibernate.Driver.SQLite20Driver");
properties.Add("hibernate.dialect",
"NHibernate.Dialect.SQLiteDialect");
properties.Add("hibernate.connection.provider",
typeof(SQLiteInMemoryTestingConnectionProvider).FullName
+ "," +

typeof(SQLiteInMemoryTestingConnectionProvider).Assembly.FullName);
properties.Add("hibernate.connection.connection_string",
"Data Source=:memory:;Version=3;New=True;");

Cheers
Christian
--
Christian Wuerdig
SOFTWARE DEVELOPER

*Compac Sorting Equipment*
T: +64 9 634 0088 ext 772
F: +64 9 634 4491

Compac
www.compacsort.com <http://www.compacsort.com>

uluho...@yandex.ru

unread,
Oct 6, 2008, 4:53:38 AM10/6/08
to Castle Project Users
Brilliant, thanks a lot!!

ulu

On Oct 5, 11:17 pm, Christian Wuerdig <chris....@gmx.de> wrote:
> SQLite in memory is a little bit tricky because every time you close a
> session usually the connection is closed and then your database is gone
> Ayende has a blogged about that:http://ayende.com/Blog/archive/2006/10/14/UnitTestingWithNHibernateAc...
>
> For me this doesn't work out of the box, because sometimes I have to
> test a presenter, which uses sessions internally. But I found a good
> workaround here:http://notepad2.wordpress.com/2008/05/19/unit-testing-castle-active-r...
> >> Ken Egozi.http://www.kenegozi.com/bloghttp://www.musicglue.comhttp://www.castle...
Reply all
Reply to author
Forward
0 new messages