使用 MSTest 和 Moq 测试存储库 - .Verify 不起作用

发布于 2024-10-17 19:25:43 字数 696 浏览 5 评论 0原文

我目前正在考虑使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梓梦 2024-10-24 19:25:43
_timerRepository.Verify(x => x.Insert(It.IsAny<Timer>()));

这满足了我的需要。它检查是否已使用某种类型的计时器触发了插入并且不是特定的(因为它不能是在其他地方创建的对象)

_timerRepository.Verify(x => x.Insert(It.IsAny<Timer>()));

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文