Is there an in-memory provider for Entity Framework?

前端 未结 8 2335
情书的邮戳
情书的邮戳 2020-12-05 22:42

I am unit testing code written against the ADO .NET Entity Framework. I would like to populate an in-memory database with rows, and make sure that my code retrieves them pro

8条回答
  •  感动是毒
    2020-12-05 23:22

    I am not familiar with Entity Framework and the ObjectQuery class but if the Include method is virtual you can mock it like this:

    // Arrange
    var customerSourceStub = MockRepository.GenerateStub>();
    var customers = new Customer[] 
    {
        // Populate your customers as if they were coming from DB
    };
    customerSourceStub
        .Stub(x => x.Include("Order"))
        .Return(customers);
    var sut = new CustomerService(customerSourceStub);
    
    // Act
    var actual = sut.GetCustomerById(5);
    
    // Assert
    Assert.IsNotNull(actual);
    Assert.AreEqual(5, actual.Id);
    

提交回复
热议问题