使用 MSTest 和 Moq 测试存储库 - .Verify 不起作用
我目前正在考虑使用 Moq 为基本执行计时器编写单元测试。当计时器停止时会调用一个函数,它将所有计时添加到数据库中,我需要测试插入是否已被调用。 我已经使用类似的测试来通过家庭控制器测试插入,但这是直接完成的。
// 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 。我不太确定为什么它返回时没有被调用?
任何想法都会很棒。
谢谢
I am currently looking at writing unit tests for a basic execution timer with Moq. There is a function called when the timer is stopped which adds all timings into the database and I need to test that the insert has been called.
I have used a similar kind of test to test inserts via the homecontroller but this is being done directly.
// Calls AddToLog() which iterates through the list
// adding all entries to the database
_timer.Stop();
_repository.Verify(x => x.Insert(TimerObject));
The error I am receiving is:
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()
the addToLog() method is defiantly being called and is calling the .insert for the repository. I'm not really sure why it comes back as not being called?
Any ideas would be great.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这满足了我的需要。它检查是否已使用某种类型的计时器触发了插入并且不是特定的(因为它不能是在其他地方创建的对象)
This does the trick for what I needed. It checks to see if an insert has been triggered with a type of Timer and is not specific (Since it cant be as the object is created else where)