嗨我收到错误,我不明白为什么
[SetUp]
public void Setup()
{
visitService = new Mock<IVisitService>();
visitRepository = new Mock<IVisitRepository>();
visitUIService = new VisitUIService(visitRepository.Object, visitService.Object);
}
[Test]
public void VisitUIService_CanSoftDelete()
{
Mock<IVisitEntity> mockVisitEntity = new Mock<IVisitEntity>();
visitService = new Mock<IVisitService>();
visitRepository.Setup(x => x.GetVisitsByDocumentLineItems(It.IsAny<IEnumerable<int>>())).Returns(new List<IVisitEntity>() { mockVisitEntity.Object});
visitUIService.DeleteVisits(new VisitDeletionModel());
visitService.Verify(x => x.SoftDeleteVisit(It.IsAny<IVisitEntity>()),Times.AtLeastOnce());
}
未对模拟执行调用:x =&gt; x.SoftDeleteVisit(IsAny())
我无法修复此问题我添加了visitService.Setup(x =&gt; x.SoftDeleteVisit(mockVisitEntity.Object))。Verifiable();以及参数的一些其他变化,但没有运气
谢谢
答案 0 :(得分:0)
我认为问题是消费对象visitUIService已经使用初始模拟接口进行了初始化,并且您稍后进行的设置无用。
两种方法:
a)在设置界面后将类的初始化移至测试
b)延迟按如下方式加载模拟,但您需要使用Func或Lazy修改类。我将使用Func
显示它visitUIService = new VisitUIService(()=>visitRepository.Object, ()=>visitService.Object);