Mocking a generic ObjectSet or IObjectSet for Unit Testing Entity Framework Business Logic

Mocking a generic ObjectSet or IObjectSet for Unit Testing Entity Framework Business Logic

test

Up until this point, I had mostly been using nHibernate for my ORM solution in most applications I work on. So when I started diving into Entity Framework (EF) models and trying to write unit tests against logic that lives in EF entities, I first encountered the ObjectSet<T> type.

Now to be clear, I was not writing unit tests against EF generated code, but rather against some custom business logic that we had living in a partial class of an EF entity.

In order to test this logic, I had to have a way to create an ObjectSet. Well…I noticed that this entity did not implement any interfaces by default, so I would first need to have it implement IObjectSet<T>.

Once I did that, I could now create a mock of that type. Using Moq (my favorite .NET mocking library), I created a mock of that type, but realized it was always empty when enumerating over it. There did not appear to be an easy way to hydrate the mock, so to speak. *By the way if anyone has successfully mocked an IObjectSet type using Moq, please share how you did it. I would love to know, and I’m sure it’s possible.

Anyways, this is what I had to do… Using an implementation of a mock ObjectSet, provided below, I was able to use Moq to successfully CreateMany() and populate an ObjectSet for my tests.

Credit for code is given to: http://www.codeproject.com/Articles/447988/How-to-Mock-Test-an-Entity-Framework-Model-First-P’


using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Data.Objects;

namespace MyApplication
{
    public partial class MockObjectSet<T> : IObjectSet<T> where T : class
    {
        private readonly IList<T> collection = new List<T>();

        #region IObjectSet<T> Members

        public void AddObject(T entity)
        {
            collection.Add(entity);
        }

        public void Attach(T entity)
        {
            collection.Add(entity);
        }

        public void DeleteObject(T entity)
        {
            collection.Remove(entity);
        }

        public void Detach(T entity)
        {
            collection.Remove(entity);
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return collection.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return collection.GetEnumerator();
        }

        #endregion

        #region IQueryable<T> Members

        public Type ElementType
        {
            get { return typeof(T); }
        }

        public System.Linq.Expressions.Expression Expression
        {
            get { return collection.AsQueryable<T>().Expression; }
        }

        public IQueryProvider Provider
        {
            get { return collection.AsQueryable<T>().Provider; }
        }

        #endregion
    }
}

I struggled for a while, trying not to use any type of concrete fake class to do this, but ultimately I had to. So that’s why I’m wondering if anyone else was able to find a way to do this wihout any “real” implementations?

If you find yourself struggling to mock IObjectSet<T> or ObjectSet<T> for any of your unit tests, it’s probably easier to just use this dummy code to do it.

thanks and happy coding/testing!

Share Your Thoughts!

Testimonials

What My Clients Say

Karin - Hope and Future

Brent has always been very prompt and generally exceeds our expectations with his work. His maintenance fee is very reasonable. I strongly recommend him!  We get many compliments on our beautiful web site!

Dan

I contacted Krueger Web Design and Brent did everything.  He setup a test site with the changes so we could verify everything was correct before we transferred from my old hosting provider.  During the setup and conversion Brent was extremely professional and responsive. He responded to any issues the same day.

Dan Kaufman
Emily - GFCLT

We've had a great experience working with Krueger Web Design. Prompt responses, great ideas, effective work... all at a reasonable price! This was the perfect fit for our organization.

Mahmud - ACMC

I would love to recommend you to anyone! You have been very helpful when we needed you the most.

Brian - SEMNIC

Working with Krueger Web Design was effortless. Communicating what I wanted for the website and the results that I saw were identical. The customer service was always prompt and I am completely satisfied with the product.

Zach - Stadium Club

Exactly what I wanted! Fast turnaround time and very affordable. Answered all of our questions quickly and made adjustments promptly. Very easy to work with and puts everything into terminology that even I can understand!

Contact Krueger Web Design

Have a question for me? Need an estimate on some work? You can email me at brent@kruegerwebdesign.com or drop me a line using the button below:

Copyright © 2017 Krueger Web Design - All rights reserved
Web Design in Madison, WI