0
Moq庫中是否有一種機制將特定方法設置爲鬆散,以便VerifyAll不會因該方法而失敗。Moq:使用MockBehavior設置模擬的例外。嚴格
[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();
}
}
對不起,我不明白。爲什麼不爲這個特定的測試設置'MockBehavior.Loose'?你已經在這些幫助函數之外創建了模擬。 – sloth