我的仓库中有一个方法可以调用datacontext.Add方法并返回resutl.Entity,如:
var result = _dataContext.Product.Add(product);
await _dataContext.SaveChangesAsync();
return result.Entity;
现在,我想为EntityEntry<Product>
创建模拟,但出现异常:
消息:Castle.DynamicProxy.InvalidProxyConstructorArgumentsException :无法实例化类的代理: Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry`1 [[产品, 产品实体,版本= 1.0.0.0,文化=中性, PublicKeyToken = null]]。找不到无参数的构造函数。
这是我的测试方法代码:
var productMock = new Mock<EntityEntry<Product>>();
var entity = new Product{Id = 1, Name = "Bag"};
mappingMock.Setup(m => m.Entity).Returns(entity);
var dataContextMock = new Mock<DataContext>(_options);
var productMockSet = new Mock<DbSet<Product>>();
dataContextMock.Setup(a => a.Product)
.Returns(productMockSet.Object);
dataContextMock.Setup(m => m.Product.Add(It.IsAny<Product>())).Returns(productMock.Object);
我在做什么错?还是有其他方法可以断言EntityEntry?
答案 0 :(得分:0)
我认为您缺少这些模拟对象:
var iStateManager = new Mock<IStateManager>();
var model = new Mock<Model>();
var productEntityEntry = new Mock<EntityEntry<Product>>(
new InternalShadowEntityEntry(iStateManager.Object, new EntityType("Product", model.Object, ConfigurationSource.Convention)));
productEntityEntry.SetupGet(m=> m.Entity).Returns(entity);