Showing posts with label TDD. Show all posts
Showing posts with label TDD. Show all posts

Wednesday, February 03, 2010

Mocking Entity Framework ObjectSets

After finally getting my head around object-graph deletes in Entity Framework v4, I set about getting some better testing around my EF code.  The first step was updating my self-tracking entities template to have my object-sets be exposed as IObjectSets which involved a small tweak to my TT file.

I ran into some challenges using a Rhino Mock of IObjectSet, so I let Google do some work for me and I landed on Jamie Phillip’s post about mocking IObjectSet via his MockObjectSet., which was exactly what I was looking for.  Adding this class into my test library got me 90% of where I wanted to be, but I still had compile errors on the spots where I was building out my object-graphs with “.Include(“”)” calls. 

After some time digging through the EF4 code with Reflector,  I was not finding what I thought I needed, an IObjectQuery interface, which would allow me to mock the Include method.  Again, I went back to Google and ran across a MSDN Forum post which Diego Vega answered with an almost too obvious solution to my exact problem: use an extension-method for Include.

Since I was only interested in mocking out the “Include” behavior, and have my MockOjectSet return my configured entity list, I went with Diego’s first suggestion for Include, which looks like this:

public static IQueryable<T> Include<T>(this IQueryable<T> source, string path){
   var objectQuery = source as ObjectQuery<T>;
   if (objectQuery != null){
      return objectQuery.Include(path);
   }
   return source;
}

The only pain point to this approach to testing, is that I’m stuck remembering that I need to add a new extension method whenever I utilize one of the methods in the ObjectQuery<T> class – Union, Distinct, etc.

Saturday, May 16, 2009

Mocking IQueryable

On my current project, we’ve been pretty successful in mocking out all of our LINQ to SQL repository operations, with one exception. One of our new methods returns an IQueryable object. No problem I think, I’ll just set an expectation on that call and return a list of the property entity type. But that gives me a build-error that I can’t convert a List<T> to IQueryable<T>.

A quick google yields the simplest of solutions to mocking IQueryable: the “AsQueryable” extension method. So now, my mock expectation returns something like “List<T>.AsQueryable” and all is well. This finding also clued me into a whole slew of interesting looking extension methods in the System.Linq namespace…..

Thursday, December 04, 2008

Mocking the Unity Container

While trying to get some tests running for my current project (WPF, VS2008, C#, MSTest, RhinoMocks 3.5, Vista 64bit), I ran into an issue where I can’t mock out the Unity Container.

I was following the advice on Randy's Blog, but when I hit the line where I’m creating a stub of the IUnityContainer, the test blows up with the error below, and like others have mentioned, when in debug, the error doesn’t surface. Here’s the error I’m seeing:

Initialization method Test.Presenter.GivenPresenter.TestAnythingAndSeeItFail threw exception. System.BadImageFormatException: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B).

Seems I’m not the only one running into this error:

http://www.nabble.com/BadImageFormatException,-but-not-when-debugging-td18742529.html

http://groups.google.com/group/RhinoMocks/browse_thread/thread/b2235a13f920c616

Edit:

After a failed attempt to created a fake based on IUnityContainer, I ran across Roy Osherove’s post on creating an AutoMockingUnityContainer which worked perfectly for my test needs.