Moq setups overlapping. What will be if setup any case and concrete after it?

那年仲夏 提交于 2020-03-03 10:45:11

问题


I want to setup the return value for the call with any arguments except one specified case, and another return value for this one case. Does the following code provide expected behavior? Does the test pass? And is it guaranteed for other possible situation that follows the described structure?

interface ISomeInterface
{
    int SomeMethod(string param);
}

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

    Assert.AreEquel(100, mock.Object.SomeMethod("aSpecificString"));
    Assert.AreEquel(1, mock.Object.SomeMethod("anyString"));
}   

And how about mixed setups, for example when we setup 'throws exception' for any argument, but 'returns value' for some specified?

mock.Setup(m => m.SomeMethod(It.IsAny<string>()))
    .Throws<Exception>();
mock.Setup(m => m.SomeMethod("aSpecificString"))
    .Returns(100);

What behavior is expected from the setup provided in the last code example?


From the accepted answer to this question I know that

The last call wins and nullifies previous calls

But, do we have the same behavior when setups are in reverse order?


回答1:


But, do we have the same behavior when setups are in reverse order?

No (Not in this case)

After testing it, it appears that when more specific expectations are done after the more loose matches the behavior is as expected

For example

[TestMethod]
public void SomeClass_ShouldBehaveProperly_GivenSomeScenario() {
    var mock = new Mock<ISomeInterface>(); // Works for Strict or Loose
    mock.Setup(m => m.SomeMethod(It.IsAny<string>()))
        .Throws<InvalidOperationException>();
    mock.Setup(m => m.SomeMethod("aSpecificString"))
        .Returns(100);
    mock.Setup(m => m.SomeMethod("anotherString"))
        .Returns(1);

    Assert.AreEqual(100, mock.Object.SomeMethod("aSpecificString")); //PASS
    Assert.AreEqual(1, mock.Object.SomeMethod("anotherString")); //PASS
    Assert.ThrowsException<InvalidOperationException>(() => 
        mock.Object.SomeMethod("anyString")); //PASS
}

If however, the same expectations are done multiple times, the last one wins

For example

[TestMethod]
public void SomeClass_ShouldBehaveProperly_GivenSomeScenario2() {
    var mock = new Mock<ISomeInterface>();
    mock.Setup(m => m.SomeMethod(It.IsAny<string>()))
       .Throws<InvalidOperationException>();
    mock.Setup(m => m.SomeMethod("aSpecificString"))
        .Returns(100);
    mock.Setup(m => m.SomeMethod("aSpecificString"))
        .Returns(1);

    Assert.ThrowsException<InvalidOperationException>(() => 
        mock.Object.SomeMethod("anyString")); //PASS
    Assert.AreEqual(100, mock.Object.SomeMethod("aSpecificString")); //Fail
}


来源:https://stackoverflow.com/questions/60341367/moq-setups-overlapping-what-will-be-if-setup-any-case-and-concrete-after-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!