0

I've been working on a project where we use a Fluent NHibernate ORM to access an SQL Server database and show and manipulate data.

In the initial development phase we've been writing unit tests against the actual database; which has been helpful in learning NHibernate, but this is not ideal as the database won't always have what we need to test and the time to run tests starts to be too long.

So I'm hoping to try to use Mock Objects in the Unit Testing, I have little Mock experience, and right now I can't always see how to change the functionality to allow me to do this.

Here's a function I wrote yesterday (re-written to a more 'abstract' concept. But basically my code with different class names:

 public static IList<Order> GetAllOrders(long parentCompanyId)
    {
        using (var session = DbSetup.GetSession())
            {
                var idList =
                    (from p in session.Query<ParentCompanyList>() 
                     where p.ParentCompanyId == parentCompanyId 
                     select p.CompanyId)
                     .ToList<long>();

                IQuery q = session.CreateQuery("from Order as o where o.CompanyId in (:ids)");
                q.SetParameterList("ids", idList);

                var results = q.List<Order>();
                return results;
            }
    }

So I have an NHibernate.ISession which connects to the database, I'm running a query to find against a ParentCompanyList (Class fluently mapped to a database table) to fetch a list of CompanyIds for companies that are connected to the parent company, and then use this to fetch all olders from all companies associated with the parent company.

What I'm not sure of whether there is any good way to test this without going to the database.

Can I create mock objects that allow me to test this? How would I set that up? Do I need to create a mock ISession which will return the appropriate List and IOrder results? At that point it seems I'm not actually testing anything of any value if I give back what I'm actually asking for...

Am I just misunderstanding something basic here?

Dave Alger
  • 339
  • 7
  • 23
  • 3
    if i understand correctly, you want to test if your NHibernate query actually delivers the results you need, not just supply a mocked GetAllOrders that returns a list of orders. In that case i would go for an in-memory sqlite database, populated with test data upon test start. – Dirk Trilsbeek Sep 19 '12 at 08:48
  • What mocking framework do you use? – larsmoa Sep 19 '12 at 09:32
  • That's a good tip, GarlandGreene, and I think you've understood the situation and I think in-memory testing sounds like a smart idea. I am curious to hear whether anyone thinks that Mock Objects would be the way to go, though. As for my framework, I'm not really committed to any. I've been testing with Moq for the most part, but there's no reason not to change. – Dave Alger Sep 19 '12 at 10:31

1 Answers1

4

When testing interaction with a database (queries, inserts ) it is best to use a database because the query translation, constraints and the like can only be tested that way. To improve performance and have test isolation an inmemory database is often used

a blogpost from one of NHibernates contributers describing unit testing with NHibernate and sqlite

Firo
  • 30,626
  • 4
  • 55
  • 94
  • You should provide a little more content/text (even if copied) as explanation. As it stands it qualifies more as a comment than an answer. IMHO. – Christian.K Sep 19 '12 at 10:08