MOQ如果方法设置相交会发生什么? [英] MOQ what happen if method setup intersect?

查看:67
本文介绍了MOQ如果方法设置相交会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您愿意,两个设置相交或重叠时会发生什么.

What happen when two setup intersect or overlap if you prefer.

例如,在以下情况下,设置重叠,因为显然"aSpecificString"也被视为任何字符串.

For example, in the below scenario the setup overlap because obviously "aSpecificString" is also considered as any string.

Interface ISomeInterface
{
    int SomeMethod(string param);
}

[TestMethod]
public void SomeClass_ShouldBehaveProperly_GivenSomeScenario()
{
    var mock = new Mock<ISomeInterface>(MockBehavior.Strict);
    mock.Setup(m => m.SomeMethod("aSpecificString"))
        .Returns(100);
    mock.Setup(m => m.SomeMethod(It.IsAny<string>()))
        .Returns(0);

    /*the rest of the test*/
}

我想知道当它相交时会发生什么.

I wonder what happen when it intersect.

它会引发异常还是无法检测到重叠并按照添加顺序使用第一个匹配设置?

Does it throw an exception or does it fail to detect the overlap and use the first matching setup in the order they were added?

我认为最好避免设置重叠.

I think it's best to avoid overlapping setup.

推荐答案

根据在线查找的文档,最后一次呼叫获胜,并使先前的呼叫无效.

According to documentation found online, The last call wins and nullifies previous calls.

所以即使在您的情况下,测试还是要打电话给

So even in your case if the test was to call

SomeMethod("aSpecificString")

它将根据您示例中的设置返回0.

it would return 0 based on the setup in your example.

这篇关于MOQ如果方法设置相交会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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