2014-03-12 48 views
0

我想寫一些測試,但我不知道如何處理設置我moq模擬的參數。任何人都可以建議我如何正確完成這一行代碼? 非常感謝,如何moq func參數,當單元測試c#

我MOQ代碼行

enter image description here

的參數是這我不知道如何處理退貨(...)所示的類型。

參數的智能感知

enter image description here

如下所示,該方法我想MOQ出來。

的方法定義

public static async Task<CommandResult> SendSupportAsync(this IBus bus, Command command, bool withLogging = true) 

UPDATE

我試過的建議,我已經添加了嘲笑的對象,併成立了回報,但編譯器提供了以下錯誤:

An expression tree may not contain a call or invocation that uses optional arguments 
+0

我不知道MOQ嘲笑出靜態方法。我以爲你必須得到一個測試工具,例如TypedMock或Telerik的測試工具。 – TYY

回答

0

如果你想嘲笑返回值,那麼你可以創建它

var resultMock = new Mock<Task<CommandResult>>(); 

然後設置你的模擬對象,並返回它像

bus.Setup(x=>c.SendSupportAsync(new TagCommand(ID, MID))).Returns(resultMock.object); 

如果你不想嘲笑的結果,那麼你可以創建一個實例並返回它像

一個模擬
bus.Setup(x=>c.SendSupportAsync(new TagCommand(ID, MID))).Returns(objCommandResult); 
+0

對於你的情況,因爲你正在返回異步任務,Moq 4.2添加了一個ReturnsAsync()擴展方法。但我沒有使用它的經驗。 –

+0

這是一個相關的線程,討論關於返回異步任務http://stackoverflow.com/questions/21253523/setup-async-task-callback-in-moq-framework –

+0

謝謝蘇雷什,我已經更新了問題,我得到編譯器錯誤 –