簡體   English   中英

Moq - 嘲笑多個論點

[英]Moq - mocking multiple arguments

我嘗試關閉Moq頁面的一半鏈接已被破壞,包括其官方API文檔的鏈接 所以我會問這里。

我已成功使用單個“catch all”參數,如下所示:

mockRepo.Setup(r => r.GetById(It.IsAny<int>())).Returns((int i) => mockCollection.Where(x => x.Id == i).Single());

但是我無法弄清楚如何使用多個參數實現相同的行為。

mockRepo.Setup(r => r.GetByABunchOfStuff(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>())).Returns( ..... );

....是我無法弄清楚的部分。


編輯以回應喬丹:

問題是如何表示3個參數而不只是一個。

怎么轉:

(int i) => mockCollection.Where(x => x.Id == i)

成:

(int i), (string s), (int j) => mockCollection.Where(x => x.Id == i && x.SomeProp == s && x.SomeOtherProp == j)

它與單個參數幾乎相同:

.Returns((int i, string s, int x) => mockCollection.Where(x => x.Id == i && x.SomeProp == s && x.SomeOtherProp == x));

或者使用返回的泛型變體:

.Returns<int, string, int>((i, s, x) => mockCollection.Where(x => x.Id == i && x.SomeProp == s && x.SomeOtherProp == x));

我想你需要的是:

   mockRepo
       .Setup(r => r.GetByABunchOfStuff(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>()))
       .Returns<int,string,int>((id, someProp, someOtherProp) =>
           mockCollection.Where(x => x.Id == i && x.SomeProp == s && x.SomeOtherProp == x));

你是說你怎么寫正確的lambda?

mockRepo.Setup(r => r.GetByABunchOfStuff(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>()))
        .Returns((int i, string s, int i2) => doSomething() );

請參閱Mark Seeman對此問題的回答:

在Moq Callback()調用中設置變量值

它可能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM