[英]NSubstitute - Make member return result of a lambda invocation
我們都知道指定替代品的返回值的方法的標准:
mySubstitute.Method().Returns(myValue);
我需要在每次調用時都計算myValue,而不僅僅是一次。 這可能嗎?
例:
mySubstitute.Method().Returns(() => ComputeValueBasedOnSystemClockAndTheWeather());
當然,這不會編譯,因為Method()
並不真正返回lambda。
我跳到NSubstitue文檔,看到他們有一個從函數返回的頁面。
可以將調用屬性或方法的返回值設置為函數的結果。 這樣可以將更復雜的邏輯替換。 盡管這通常是一個不好的做法,但在某些情況下它很有用。
calculator
.Add(Arg.Any<int>(), Arg.Any<int>())
.Returns(x => (int)x[0] + (int)x[1]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.