我从Moq那里得到了这个错误,并没有对我有多大意义
这是我想要模拟的主要方法
public int Add(Customer customer)
{
var result = 0;
using (_entverasysEntities)
{
var doesCustExist = _entverasysEntities.tblCustomers.Any(cust => cust.CustomerName.Trim() == customer.CustomerName.Trim() && cust.IsDelete.Value == false);
if (!doesCustExist)
{
customer.CustomerId = Guid.NewGuid();
customer.CreatedBy = customer.CustomerId;
customer.UpdatedBy = customer.CustomerId;
customer.CreatedDate = DateTime.Now;
customer.UpdatedDate = DateTime.Now;
customer.IsDeleted = false;
customer.SpaceTypeId = 1;
_entverasysEntities.tblCustomers.AddOrUpdate(Mapper.Map(customer, new tblCustomer()));
result = _entverasysEntities.SaveChanges() > 0 ? (int)ReturnResult.Success : (int)ReturnResult.Failure;
if (result == 1)
{
// will be changed once the Unit Details are finalized
customer.UnitMapping.CustomerId = customer.CustomerId;
customer.UnitMapping.CustomerUnitMappingId = Guid.NewGuid();
customer.UnitMapping.CreatedBy = customer.CustomerId;
customer.UnitMapping.UpdatedBy = customer.CustomerId;
customer.UnitMapping.CreatedDate = DateTime.Now;
customer.UnitMapping.UpdatedDate = DateTime.Now;
_entverasysEntities.tblCustomerUnitMappings.AddOrUpdate(Mapper.Map(customer.UnitMapping, new tblCustomerUnitMapping()));
result = _entverasysEntities.SaveChanges() > 0 ? (int)ReturnResult.Success : (int)ReturnResult.Failure;
}
}
else
result = (int)ReturnResult.RecordExist;
}
return result;
}
单元测试
[TestMethod]
public void CustomerDataManager_AddCustomerTest()
{
var custid = Guid.NewGuid();
var customerData = new List<tblCustomer>
{
new tblCustomer {CustomerName="Enterprize",IsDelete=false},
}.AsQueryable();
var unitMappingData = new List<tblCustomerUnitMapping>
{
new tblCustomerUnitMapping { CustomerId=custid},
}.AsQueryable();
var mockContext = new Mock<ENTVERASYSEntities>();
var mockCustomer = new Mock<DbSet<tblCustomer>>();
var mockUnitMapping = new Mock<DbSet<tblCustomerUnitMapping>>();
mockCustomer.As<IQueryable<tblCustomer>>().Setup(x => x.Provider).Returns(customerData.Provider);
mockCustomer.As<IQueryable<tblCustomer>>().Setup(x => x.Expression).Returns(customerData.Expression);
mockCustomer.As<IQueryable<tblCustomer>>().Setup(x => x.ElementType).Returns(customerData.ElementType);
mockCustomer.As<IQueryable<tblCustomer>>().Setup(x => x.GetEnumerator()).Returns(customerData.GetEnumerator());
mockCustomer.SetReturnsDefault(customerData.GetEnumerator());
mockCustomer.Verify(x => x.AddOrUpdate(It.IsAny<tblCustomer>()),Times.Once());
mockContext.Verify(x => x.SaveChanges(),Times.Once());
mockUnitMapping.As<IQueryable<tblCustomerUnitMapping>>().Setup(x => x.Provider).Returns(unitMappingData.Provider);
mockUnitMapping.As<IQueryable<tblCustomerUnitMapping>>().Setup(x => x.Expression).Returns(unitMappingData.Expression);
mockUnitMapping.As<IQueryable<tblCustomerUnitMapping>>().Setup(x => x.ElementType).Returns(unitMappingData.ElementType);
mockUnitMapping.As<IQueryable<tblCustomerUnitMapping>>().Setup(x => x.GetEnumerator()).Returns(unitMappingData.GetEnumerator());
//mockUnitMapping.Verify(x => x.AddOrUpdate(new tblCustomerUnitMapping { CustomerId = custid, CustomerName = "Enterprize", CreatedBy = custid, UpdatedBy = custid, CreatedDate = DateTime.Now, UpdatedDate = DateTime.Now }));
//mockContext.Verify(x => x.SaveChanges());
mockContext.Setup(x => x.tblCustomers).Returns(mockCustomer.Object);
//mockContext.Setup(x=>x.tblCustomers.AddOrUpdate()).re
mockContext.Setup(t => t.tblCustomerUnitMappings).Returns(mockUnitMapping.Object);
var service = new CustomerDataManager(mockContext.Object);
int service2 = service.Add(new Customer { CustomerId = custid, CustomerName = "Enterprize1" ,CreatedBy=custid,UpdatedBy=custid,CreatedDate=DateTime.Now,UpdatedDate=DateTime.Now,SpaceTypeId=1});
Assert.AreEqual(2, service2);
Assert.AreEqual("Enterprize",service2);
Assert.AreEqual(custid, service2);
}
当我调试上面的测试方法时,下面这行抛出异常
mockCustomer.Verify(x => x.AddOrUpdate(It.IsAny<tblCustomer>()),Times.Once());
错误是
对非虚拟(在VB中可覆盖)成员的验证无效:
x => x.AddOrUpdate<tbl Customer>(new[] { })
我不知道如何解决这个问题,而且我正在使用EF6模型第一种方法。