Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReturnsAsync() with factory method does not behave as expected #303

Closed
sellotape opened this issue Nov 28, 2016 · 1 comment
Closed

ReturnsAsync() with factory method does not behave as expected #303

sellotape opened this issue Nov 28, 2016 · 1 comment

Comments

@sellotape
Copy link

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).

@SaroTasciyan
Copy link
Contributor

SaroTasciyan commented Dec 10, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants