2014-03-26 72 views
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(); 
    } 
} 
+0

對不起,我不明白。爲什麼不爲這個特定的測試設置'MockBehavior.Loose'?你已經在這些幫助函數之外創建了模擬。 – sloth

回答

0

兩兩件事:

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(); 

也就是爲什麼你打了嚴格的行爲?或者創建一個單獨的測試方法並單獨驗證它們?另外我建議簡單看看This Link Here以更好地理解Moq的驗證。

你的問題的直接回答是沒有起訂量庫不包含預先定義的方式來建立一個鬆散的班級但是我相信,如果你想你可以創建你自己的助手extension.B