title: How to mockup Entity Framework 6 With Moq & Autofixture
body: <p>I am using <a href="https://www.nuget.org/packages/AutoFixture.AutoMoq" rel="nofollow">AutoMoq</a> but I am kinda confused how to write my first unit test because of Entity Framework's (using EF6 and code first) <code>dbContext</code></p>
  
  <pre><code>// in service class(constructor)
  private readonly MyContext context;
  
  public PriceService(MyContext context)
  {
      this.context = context;
  }
  
  // following would be in nunit test method.
  var fixture = new Fixture().Customize(new AutoMoqCustomization());
  var priceService = fixture.Create&lt;PriceService&gt;();
  </code></pre>
  
  <p>When I run the unit test it crashes </p>
  
  <pre>
      at Ploeh.AutoFixture.Kernel.TerminatingSpecimenBuilder.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.AutoPropertiesCommand`1.Execute(Object specimen, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
      at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.MethodInvoker.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.c__DisplayClass6.b__1(ISpecimenBuilder b)
      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.d__a5`1.MoveNext()
      at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
      at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
      at Ploeh.AutoFixture.SpecimenFactory.Create[T](ISpecimenContext context, T seed)
      at Ploeh.AutoFixture.SpecimenFactory.Create[T](ISpecimenContext context)
      at PriceServiceTests.Test_Price_Object_Setup() in PriceServiceTests.cs:line 26
  </pre>
  
  <p>Edit</p>
  
  <p>In EF 6 seems like they are making the DbSet more mockable.</p>
  
  <p><a href="https://entityframework.codeplex.com/wikipage?title=Design%20Meeting%20Notes%20-%20May%2016,%202013" rel="nofollow">https://entityframework.codeplex.com/wikipage?title=Design%20Meeting%20Notes%20-%20May%2016%2c%202013</a></p>
  
  <blockquote>
    <p>Make DbSet more mockable</p>
    
    <ul>
    <li>These means adding a protected constructor and making methods virtual</li>
    <li>Note that a type derived from DbSet that uses the protected constructor would create an object not be bound to any context and the methods would be no-ops. This makes it very like IDbSet from the perspective of creating test doubles.</li>
    <li>If we took this option we could potentially obsolete IDbSet</li>
    <li>It’s worth noting that no cases have been identified where this would be functionally different to using IDbSet for test doubles. However, there is strong feeling in the community that interfaces are preferred.</li>
    </ul>
  </blockquote>
  
  <p>Anyone know how to mock it up?</p>
  
  <p>Edit 2</p>
  
  <p>I found <a href="http://msdn.microsoft.com/en-us/data/dn314429.aspx" rel="nofollow">this article</a> but it keeps crashing</p>
  
  <pre><code>public class MyContext : DbContext
  {
      //public GroceryListContext()
      //    : base()
      //{
  
      //}
      public virtual DbSet&lt;Price&gt; Prices { get; set; }
  }
  
  [Test]
  public void Test_Price_Object_Setup_Properly()
  {
      var mockContext = new Mock&lt;MyContext&gt;();
  
      var mockSet = new Mock&lt;DbSet&lt;Price&gt;&gt;(); // had to add EF to my test solution.
      mockContext.Setup(m =&gt; m.Prices).Returns(mockSet.Object);
      var service = new PriceService(mockContext.Object);
  
      // dies when using autofixture so thought try first moq like in article
     //var priceService = fixture.Create&lt;PriceService&gt;();
  
     Assert.That(true, Is.EqualTo(false));
  }
  </code></pre>
  
  <p>with the following exception:</p>
  
  <blockquote>
    <p>MyContext.Tests.Services.PriceServiceTests.Test_If_Price_Object_Setup_Properly:  <code>System.ArgumentException : Type to mock must be an interface or an abstract or non-sealed class.    ----&gt; System.TypeLoadException : Method 'Create' on type 'DbSet</code>1Proxyb409fc6b430b4568aac048b60ea2f02e' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a621a9e7e5c32e69' tried to implicitly override a method with weaker type parameter constraints`.</p>
  </blockquote>