Reputation: 359
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?
Upvotes: 0
Views: 1658
Reputation: 30813
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
Upvotes: 4