mercredi 27 juillet 2016

Implementing a stub for NHibernate Fetch extension method on List.AsQuerable

I am developing some database applications using FluentNhibernate.

In my unit tests, I am mocking all my database operations (basically LINQ code) by presenting a plain generic list in memory as the querable object instead of getting an IQuerable from the nhibernate session instance.

//AAA: Arrange, Act, Assert

//arrange
var myObjects = new List<MyObject>();
//add test objects...
var businessLogic = new BusinessLogic(myObjects.AsQueryable);

//act
businessLogic.Execute();

//Assert
Assert.AreEqual(2,myObjects.Count);
Assert.IsTrue(myObjects[0].Modified);
...

With this, i am able to use mostly all of the LINQ extension methods that are, say, "plain vanilla". An example:

public void BusinessLogic.Execute(IQuerable<MyObject> objects)
{
   objectsToProcess = objects.Where(x=>x.ToBeProcessed))
   foreach(var obj on objectsToProcess)
   {
       obj.Modified = true;
   }
}

My problem: If the objects to be tested are using Nhibernate specific extension methods (such as "Fetch" to retrieve associated detail objects), the mocked queries executed on the list instances fail with the following exception (translated from german):

System.InvalidOperationException: There is no Fetch method on type "NHibernate.Linq.EagerFetchingExtensionMethods" matching with the arguments provided.

Im not very familiar with the whole "Queryable" theme, so excuse any silly question... Is there a way to build a stub that just ignores such commands when triggered on List instances? As this problem is one that is raised at runtime only, there is some reflection going on, i suppose?

Aucun commentaire:

Enregistrer un commentaire