Dot-Net
驗證使用 Moq 呼叫受保護方法的次數
在我的單元測試中,我正在使用 Moq 模擬一個受保護的方法,並且想斷言它被呼叫了一定次數。這個問題描述了早期版本的 Moq 的類似內容:
//expect that ChildMethod1() will be called once. (it's protected) testBaseMock.Protected().Expect("ChildMethod1") .AtMostOnce() .Verifiable(); ... testBase.Verify();但這不再有效;從那時起語法發生了變化,我找不到使用 Moq 4.x 的新等效項:
testBaseMock.Protected().Setup("ChildMethod1") // no AtMostOnce() or related method anymore .Verifiable(); ... testBase.Verify();
在Moq.Protected命名空間中,有一個IProtectedMock介面,它有一個以 Times 作為參數的 Verify 方法。
編輯 這至少從最小起訂量 4.0.10827 開始可用。語法範例:
testBaseMock.Protected().Setup("ChildMethod1"); ... testBaseMock.Protected().Verify("ChildMethod1", Times.Once());