在RhinoMocks中,您可以將您的嘲聲告訴IgnoreArguments作爲一攬子聲明。在Moq中,似乎必須爲每個參數指定It.IsAny()。但是,這不適用於ref和out參數。如何測試下面的方法,我需要MOQ內部服務調用返回一個特定的結果:如何讓Moq忽略參考或輸出的參數
public void MyMethod() {
// DoStuff
IList<SomeObject> errors = new List<SomeObject>();
var result = _service.DoSomething(ref errors, ref param1, param2);
// Do more stuff
}
測試方法:
public void TestOfMyMethod() {
// Setup
var moqService = new Mock<IMyService>();
IList<String> errors;
var model = new MyModel();
// This returns null, presumably becuase "errors"
// here does not refer to the same object as "errors" in MyMethod
moqService.Setup(t => t.DoSomething(ref errors, ref model, It.IsAny<SomeType>()).
Returns(new OtherType()));
}
UPDATE:那麼,從「裁判」改變錯誤「出」的作品。所以看起來真正的問題是有一個ref參數,你不能注入。
你能因爲你的樣品有3個參數'出錯誤,參數1,param2'但在測試你與兩個參數調用它'出錯誤,It.IsAny在發佈DoSomething'的'簽名()'也許你在嘲笑一個錯誤的重載,因爲你的代碼無論如何應該工作,看[moq幫助方法部分](http://code.google。COM/P /最小起訂量/維基/快速啓動)。 –
nemesv
這只是一個例子 - 但我已經更新了測試並行的示例代碼 – sydneyos