在Moq库中是否有一种机制来设置一个特定的方法为Loose,以便VerifyAll对该方法不会失败?
[TestFixture]
public class MockStrictException
{
[Test]
public void exception_to_setup_strict()
{
var mock = new Mock<ITest>(MockBehavior.Strict);
SetupAsPartOfTestSuite(mock);
ITest subject = mock.Object;
//Act
subject.Called().Should().Be(10);
mock.VerifyAll();
}
//Contrived example, however is actual usage there are setup helper methods
//I understand it should not have been setup as strict
//but way too much effort to fix all test cases
private static void SetupAsPartOfTestSuite(Mock<ITest> mock)
{
mock.Setup(x => x.Called()).Returns(10);
//TODO: Is there a way to override this setup
mock.Setup(x => x.NotCalled()).Returns(-10);
}
public interface ITest
{
int Called();
int NotCalled();
}
}
2件事:
mock.Setup(x => x.Called()).Returns(10);
//TODO: Is there a way to override this setup
mock.Setup(x => x.NotCalled()).Returns(-10);
如果您将其更改为:
,使它们可验证,这将是有意义的。 mock.Setup(x => x.Called()).Returns(10).Verifiable();
mock.Setup(x => x.NotCalled()).Returns(-10).Verifiable();
也为什么你叫严格的行为?或者创建一个单独的测试方法并单独验证它们?此外,我建议简要地看看这个链接,以更好地理解Moq的验证。
你的问题的直接答案是no Moq库不包含一个预定义的方式来创建一个松散的类,但我敢肯定,如果你想,你可以创建你自己的作为一个助手扩展。B