MOQ如果方法设置相交会发生什么?

AXM*_*MIM 1 c# unit-testing moq

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

例如,在下面的场景中,设置重叠,因为显然"aSpecificString"也被视为任何字符串.

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*/
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

cod*_*ife 5

不要沿着你走的路走下去.我只看到了大量的痛苦.相反,让你的Return条件:

mock.Setup(m => m.SomeMethod(It.IsAny<string>()))
    .Returns((string parameter) => parameter == "aSpecificString" ? 100 : 0);
Run Code Online (Sandbox Code Playgroud)

我希望在你开始放下重要的代码之前我能找到你的...