0

I'm working on project that uses NHibernate 3, but the code is pretty messy. What I would like to do is:

  • introduce testing code into project
  • introduce Repository pattern - I did it in projects with Entity Framework 4, and I enjoyed it, especially opportunity to test queries regardless of storage used (I could mock the internal storage and inject in-memory implementation like List of objects)
  • as currently DAO code works with CreateCriteria methods and creates queries that are NHibernate specific, it's impossible to replace NHibernate storage with another (or at least I don't know how to execute NHibernate.ICriteria on List instance. So I need a way to create queries that can be used regardless of storage implementation.

Is there any way to achieve it?

Ian Nelson
  • 57,123
  • 20
  • 76
  • 103
dragonfly
  • 17,407
  • 30
  • 110
  • 219

1 Answers1

0

It is probably not exactly what you are looking for. We have two kinds of tests: unit tests, which test pure logic without database and database integration tests.

Unit Tests

We have interfaces to classes that implement the queries. This way, the domain does not have any references to NHibernate. In your case, this are probably the repositories. Write simple and small interfaces to them and mock the whole repository. You don't even need NHibernate for this. That's the beauty of persistence ignorance.

Database Integration Tests

We are running NHibernate on an Sqlite database which is in memory. This way we are testing the mapping, queries and other stuff that depends on NHibernate.

Community
  • 1
  • 1
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • It's related to Unit Tests. What you wrote is mocking the results of mocked repository calls - for instance FindByCity('Chicago') returns 10 cities. However what I want to achieve, is to test whether complex queries are correctly handled. For instance FindBy(new QueryByCity('Chicago')) returns 10 cities - in order to check if query in correctly implemented I want to mock internal context/in-memory storage to return, for instance, 20 cities, and repository will apply query and return cities that actually match the query. – dragonfly May 21 '12 at 13:15
  • 1
    Hmmm, so actually, after reading once again you answer, it seems that what I'm doing is Database Integration Tests. Is that right? – dragonfly May 21 '12 at 13:20
  • @dragonfly: yes, it would be a database integration test. It requires the whole thing: database, NH, data, but then it also works for any kind of query, even (standard) SQL. – Stefan Steinegger May 21 '12 at 13:28