MOQ'ing方法调用顺序 [英] MOQ'ing method call sequence

查看:71
本文介绍了MOQ'ing方法调用顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在模拟用于某些企业库调用的包装器.世界上一切都很好,我的测试通过了,实际的代码起作用了!

I am mocking out a wrapper we use for some enterprise library calls. All was well in the world, my tests passed and the actual code worked!

但是,我随后扩展了功能,并验证了所有测试仍然通过.宾果游戏-他们做到了.但是他们不在现实世界中,因为有一种期望

However I then extended the functionality, and verified the tests all still passed. Bingo - they did. However they didn't in the real world because there is an expectation that

InitialiseDBCommand(string, commandtype)

将在之前被调用

AddCmdParameter(string, dbtype, object)

所以像个好男孩一样,我要做的第一件事就是编写一个表现出这种行为并期望抛出异常的测试.

So like a good boy, the first thing I want to do is write a test that exhibits this behavior and expects an exception to be thrown.

如果没有使用任何字符串调用InitialDBCommand,我需要组装AddCmmParater引发异常.

What I need is to rig up AddCmmParater to throw an exception IF InitialDBCommand has not been called with any string.

我认为我可以通过回调来做到这一点,但感觉应该添加一个方法调用序列(而不是方法返回序列).

I figure I could do this with a call back, but it feels like moq'ing a method call sequence (not a method return sequence) ought to be there.

类似

iDataAccessHelper.Setup(s=>s.AddCmdOutputParameter(It.IsAny<string>(), 
                  It.IsAny<DbType>(), 
                  It.IsAny<int>()))
              .When(w=>w.InitialiseDBCommand(etc etc)
              .Throws<NullReferenceException>()

有指针吗?

推荐答案

有一个扩展Moq以添加此功能的库,称为 Moq序列.从他们的示例中看,就像

There is a library that extends Moq to add this functionality called Moq Sequences. Which from their examples looks like

using (Sequence.Create())
{
    mock.Setup(_ => _.Method1()).InSequence();
    mock.Setup(_ => _.Method2()).InSequence(Times.AtMostOnce());
    ...
    // Logic that triggers the above method calls should be done here.
    ...

}";

它断言Method1在Method2之前被调用

It asserts that Method1 is called before Method2

这篇关于MOQ'ing方法调用顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆