[英]NSubstitute: To be able to set the reference object in the mocked method that has no return type
我有一個帶有以下聲明的接口:
void MapServiceMessages(IEnumerable<ServiceMessage> serviceMessages, List<Message> responseMessages);
我想模擬這個方法,我發送一個返回消息類型列表的servicemessage列表。 由於它是void類型,那么我該如何模擬這個方法。
我不想改變我的聲明既不定義。
當然我可以選擇將void更改為List然后使用(...)。返回(mychoiceofmessages)....
我想與社區核實他們是否遇到過這樣的問題和更好的解決方案。
謝謝,
來自NSubstitute回調的文檔
返回()可用於獲取返回值的成員的回調,但對於void成員,我們需要不同的技術,因為我們無法在void返回上調用方法。 對於這些情況,我們可以使用When..Do語法
public interface IFoo {
void SayHello(string to);
}
[Test]
public void SayHello() {
var counter = 0;
var foo = Substitute.For<IFoo>();
foo.When(x => x.SayHello("World"))
.Do(x => counter++);
foo.SayHello("World");
foo.SayHello("World");
Assert.AreEqual(2, counter);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.