我目前正在考虑使用 Moq 为基本执行计时器编写单元测试。计时器停止时会调用一个函数,它将所有时间添加到数据库中,我需要测试插入是否已被调用。 我使用了一种类似的测试来通过家庭 Controller 测试插入,但这是直接完成的。
// Calls AddToLog() which iterates through the list
// adding all entries to the database
_timer.Stop();
_repository.Verify(x => x.Insert(TimerObject));
我收到的错误是:
Expected invocation on the mock at least once, but was never performed: x => x.Insert(.splitTimer)
Performed invocations:
ITimersRepository.Insert(EF.Domain.Entities.Timer)
ITimersRepository.Insert(EF.Domain.Entities.TimerSplit)
ITimersRepository.Save()
addToLog() 方法被挑衅地调用,并且正在为存储库调用 .insert。我不太确定为什么它没有被调用而返回?
任何想法都会很棒。
谢谢
最佳答案
_timerRepository.Verify(x => x.Insert(It.IsAny<Timer>()));
这可以满足我的需要。它检查是否已使用 Timer 类型触发了插入并且不是特定的(因为它不能在其他地方创建对象)
关于c# - 使用 MSTest 和 Moq 测试存储库 - .Verify 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5042547/