1: public class Customer
2: {3: public int Id { get; set; }
4: public string Name { get; set; }
5: } 6: 7: public class CustomerContext : DbContext, ICustomerContext
8: {9: public DbSet<Customer> Customers { get; set; }
11: }I now want to write some tests and I'm going to mock the DbContext using NSubstitute. I first try something like this:
1: [Test]2: public void can_mock_customer_context()
3: { 4: var context = Substitute.For<CustomerContext>(); 5: context.Customers.Returns(6: new DbSet<Customer>(
7: new[]
8: {9: new Customer {Name = "Sean"}
10: }) 11: ); 12: Assert.AreEqual(1, context.Customers.Count()); 13: }The problem is that the DbSet constructor is internal (as of EF4 Code First CTP5). So, let's abstract our DB access to a simple interface and replace DbSet<T> with the IQueryable<T> interface, ending up with the below:
1: public interface ICustomerContext
2: { 3: IQueryable<Customer> Customers { get; set; } 4: }This interface can be implemented like so:
1: public class CustomerContext : DbContext, ICustomerContext
2: {3: public DbSet<Customer> Customers { get; set; }
4: IQueryable<Customer> ICustomerContext.Customers { get { return Customers; } }
5: }Now all we need to do is to use an implementation of IQueryable<T> in our mock. I'm going to use EnumerableQuery<T> which gives me the following test that now passes:
1: [Test]2: public void can_mock_customer_context()
3: { 4: var context = Substitute.For<ICustomerContext>(); 5: context.Customers.Returns(6: new EnumerableQuery<Customer>(
7: new[]
8: {9: new Customer {Name = "Sean"}
10: }) 11: ); 12: Assert.AreEqual(1, context.Customers.Count()); 13: } 14: }I'm new to NSubstitute but it seem to be the lowest friction mocking library out there. Just perfect for use with Entity Framework 4 Code First - certainly the lowest friction ORM there is today!
Note that we could have used the repository pattern to wrap the DbContext instead of a simple interface, the approach is almost identical.
Update: Ro Miller has an alternative approach using fakes that does a better job of surfacing IDbSet