Mocking NHibernate & Linq

I use NHibernate and open a session in my interface. I have a controller action that restores tasks as follows:

public ActionResult Overview(DateTime date) { var allTasks = GetTasksUpUntilDate(date); return PartialView("Tasks/Overview", allTasks); } private List<TaskOverviewModel> GetTasksUpUntilDate(DateTime date) { var allTasks = _session.Query<Task>().Where(t.BookedBy.UserName.Equals(CurrentUser.Name, StringComparison.CurrentCultureIgnoreCase)); var tasks = allTasks.Where(t => t.DueDate <= date); var taskVMs = new List<TaskOverviewModel>(); tasks.ForEach(t => taskVMs.Add(MapEntityToViewModel(t))); return taskVMs; } 

Now I do not want to create an IRepository for my views only, since ISession is actually already a repository. This is taunting, but it is rather complicated. So can someone help me have _session.Query return a list of objects that I provide during testing?

I would also like to avoid creating a database in memory and use RhinoMocks for my tests.

+7
source share
2 answers

Do not fake Nh / linq. Instead, configure the SQL query in sqlite for the query. They are extremely fast and easy to use.

+5
source

The NHibernate session may follow the repository pattern, but if you create your own controllers to communicate directly with it, you are not really abstracting it. Mocking this when you are not abstracting is not a reliable solution.

If you absolutely do not want to abstract it (this is pure laziness, IMO), then sqllite db, which Jason mentioned, is a good call. However, for a large project, appropriately sharing your concerns is a very good idea.

My domain model contains interfaces for both data access objects (REPOs) and services that consume them. This allows me to truly separate my data from my business tasks, which should be completely separate from the problems associated with the presentation / application. This allows you to properly test the device and the ability to easily replace parts or make the right mockery.

Each layer ONLY speaks with interfaces, NEVER to implement. In addition, my application layer never speaks directly to the data layer - only services. Believing this to happen, they seem to encourage developers to become lazy and begin to introduce business or data logic into the application.

+2
source

All Articles