为什么DBSet Add失败,还会破坏DBSet中现有的Customer条目?

时间:2014-11-18 15:01:58

标签: entity-framework mocking moq in-memory-database dbset

我们的编程涉及使用内存数据进行的一些模拟测试。

      // Let us create some in-memory data
        // Create a list of Customer
        List<Customer> listOfCustomers =  new List<BlahProjectBlahExample.Domain.Objects.Customer>()
                                   { new Customer { CustomerID = "1    ", CompanyName = "Chicago Bulls", ContactName = "Michael Jordan", ContactTitle = "top basket ball player", Address = "332 testing lane", City = "Chicago", Region = "Illinois", PostalCode = "484894", Country = "USA", Phone = "3293993", Fax = "39393" },
                                     new Customer { CustomerID = "2    ", CompanyName = "Miami Heat", ContactName = "Lebron James", ContactTitle = "second best basket ball player", Address = "90 test street", City = "Miami", Region = "Florida", PostalCode = "4869394", Country = "USA", Phone = "3293213", Fax = "33393" },
                                     new Customer { CustomerID = "3    ", CompanyName = "Oklahoma City Thunder", ContactName = "Kevin Durant", ContactTitle = "current top basket ball player", Address = "35 test row", City = "Oklahoma City", Region = "Oklahoma", PostalCode = "480290", Country = "USA", Phone = "304923", Fax = "33325" }
                                   };
        // Convert the list to an IQueryable list
        IQueryable<Customer> queryableListOfCustomerInMemoryData = listOfCustomers.AsQueryable();


        // Let us create a Mocked DbSet object.
        Mock<DbSet<BlahProjectBlahExample.Domain.Objects.Customer>> mockDbSet = new Mock<DbSet<BlahProjectBlahExample.Domain.Objects.Customer>>();

     // Force DbSet to return the IQueryable members
        // of our converted list object as its 
        // data source
        mockDbSet.As<IQueryable<BlahProjectBlahExample.Domain.Objects.Customer>>().Setup(m => m.Provider).Returns(queryableListOfCustomerInMemoryData.Provider);
        mockDbSet.As<IQueryable<BlahProjectBlahExample.Domain.Objects.Customer>>().Setup(m => m.Expression).Returns(queryableListOfCustomerInMemoryData.Expression);
        mockDbSet.As<IQueryable<BlahProjectBlahExample.Domain.Objects.Customer>>().Setup(m => m.ElementType).Returns(queryableListOfCustomerInMemoryData.ElementType);
        mockDbSet.As<IQueryable<BlahProjectBlahExample.Domain.Objects.Customer>>().Setup(m => m.GetEnumerator()).Returns(queryableListOfCustomerInMemoryData.GetEnumerator());



         Mock<BlahProjectBlahDataContext> mockedReptryCtxt = new Mock<BlahProjectBlahDataContext>();


         mockedReptryCtxt.Setup(q => q.Customers).Returns(mockDbSet.Object);
         mockedReptryCtxt.Setup(q => q.Set<Customer>()).Returns(mockDbSet.Object);
        mockedReptryCtxt.CallBase = true;




        DbSet<Customer> inMemoryDbSetCustomer = mockedReptryCtxt.Object.Set<Customer>();

在下面的代码中,我使用了一个循环来查看inMemoryDbSetCustomer的内容,它包含了预期的数据。

      Customer something;
      foreach (var entry in inMemoryDbSetCustomer)
      {
          something = entry as Customer;
      }

可悲的是,当我尝试添加新客户时, 1)inMemoryDbSetCustomer无法添加客户 2)inMemoryDbSetCustomer.Add(someCust)返回NULL 3)inMemoryDbSetCustomer似乎丢失了所有其他客户条目。

        Customer someCust = new Customer { CustomerID = "4    ", CompanyName = "Kolkota Knights", ContactName = "Sachin Tendulkar", ContactTitle = "current top cricket player", Address = "35 test row", City = "Kolkota", Region = "West Bengal", PostalCode = "3454534", Country = "India", Phone = "304923", Fax = "33325" };
        try
        {
            Customer returnCust = (Customer)(inMemoryDbSetCustomer.Add(someCust));
        }
        catch(Exception ex ){

        }

为什么DBSet Add失败,还会破坏DBSet中现有的Customer条目?

以答案更新

感谢@Werlang的建议。以下代码添加有助于:

 mockDbSet.Setup(m => m.Add(It.IsAny<Customer>()))
                   .Callback<Customer>((Customer c) => { listOfCustomers.Add(c); })
                   .Returns((Customer c) => c);

此外,如果您像我一样使用Moq Framework模拟DBSets,那么以下stackoverflow发布将会有所帮助,因为您可能会面临类似的问题:

What steps to get rid of Collection was modified; enumeration operation may not execute. Error?

1 个答案:

答案 0 :(得分:0)

当通过IQueryable访问时,您已经模拟了可查询器返回一组固定数据。但是,以DbEntry的形式,DbContext内部的单独列表中保存了Add。

我建议您也模拟Add()方法,将新元素添加到模拟数组(listOfCustomers)。