You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a bug in ReturnsAsync<TMock, TResult>(this IReturns<TMock, Task<TResult>> mock, Func<TResult> value):
Returns(() => f()) behaves as one would expect; i.e. call f() each time the method that was setup is called, to determine the value.
ReturnsAsync(() => f()), however, returns the same value every time the setup method is called. Looking at the code, the reason why is clear: ReturnsAsync() evaluates f() at Setup() time, rather than when the setup method is called, as one would expect:
public static IReturnsResult<TMock> ReturnsAsync<TMock, TResult>(this IReturns<TMock, Task<TResult>> mock, Func<TResult> value) where TMock : class
{
return mock.ReturnsAsync(value());
}
Workaround: drop back to .Returns(() => Task.FromResult(f()))
(That's the code fix as well, BTW).
The text was updated successfully, but these errors were encountered:
I experienced the same issue yesterday, digging into the code I realized that the func is evaluated right away and Task.FromResult(...) is built with given value.
However, in the documentation for Returns(Func<T, TResult>) it's stated that "returns a calculated value which is evaluated lazily at the time of the invocation"
I agree with your fix suggestion, PR is ready to go.
There is a bug in
ReturnsAsync<TMock, TResult>(this IReturns<TMock, Task<TResult>> mock, Func<TResult> value)
:Returns(() => f())
behaves as one would expect; i.e. callf()
each time the method that was setup is called, to determine the value.ReturnsAsync(() => f())
, however, returns the same value every time the setup method is called. Looking at the code, the reason why is clear:ReturnsAsync()
evaluatesf()
atSetup()
time, rather than when the setup method is called, as one would expect:Workaround: drop back to
.Returns(() => Task.FromResult(f()))
(That's the code fix as well, BTW).
The text was updated successfully, but these errors were encountered: