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!