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

Missing ReturnsExtensions for Methods which returns Task #117

Closed
Lasmais opened this issue Jul 1, 2014 · 5 comments
Closed

Missing ReturnsExtensions for Methods which returns Task #117

Lasmais opened this issue Jul 1, 2014 · 5 comments

Comments

@Lasmais
Copy link

Lasmais commented Jul 1, 2014

Moq library has extensions in Moq.ReturnsExtensions for mocked methods which returns Task<> but none for the one with simple Task. Can this feature be added to moq library? Here is example implementation.

public static class ReturnsExtensions
{
    public static IReturnsResult<TMock> ReturnsAsync<TMock>(this IReturns<TMock, Task> mock) where TMock : class
    {
        var tcs = new TaskCompletionSource<object>();
        tcs.SetResult(null);

        return mock.Returns(tcs.Task);
    }
}
@abatishchev
Copy link
Contributor

Just edit the source online and make a pull request from the same page.

Also here's an alternative implementation:

// pure non-generic Task
var task = Task.Factory.StartNew(() => { });
return mock.Returns(task);

or

// not null result
var tcs = new TaskCompletionSource<bool>();
tcs.SetResult(true);
return mock.Returns(tcs.Task);

Lasmais added a commit to Lasmais/moq4 that referenced this issue Jul 2, 2014
Added missing ReturnsExtensions for Methods which returns Task devlooped#117
@kzu
Copy link
Member

kzu commented Jul 2, 2014

You can also just return a Task.Delay(0)

/kzu

Daniel Cazzulino | Team Lead | Xamarin for Visual Studio

On Tue, Jul 1, 2014 at 4:43 PM, Alexander Batishchev <
notifications@github.com> wrote:

Just edit the source online
https://github.com/Moq/moq4/edit/master/Source/ReturnsExtensions.cs and
make a pull request from the same page.

Also here's an alternative implementation:

// pure non-generic Task
var task = Task.Factory.StartNew(() => { });
return mock.Returns(task);

or

// not null result
var tcs = new TaskCompletionSource();
tcs.SetResult(true);
return mock.Returns(tcs.Task);

Reply to this email directly or view it on GitHub
#117 (comment).

@kzu
Copy link
Member

kzu commented Jul 2, 2014

There is no need for this feature.

In .NET 4.5 you can just use the new members of Task: Task.Delay, Task.FromResult.

In .NET 4.0, you can just install TaskHelpers.Sources, and have the same (even more!) with TaskHelpers.Completed(), TaskHelpers.FromResult() and many more.

@kzu kzu closed this as completed Jul 2, 2014
@paolofulgoni
Copy link

paolofulgoni commented Mar 17, 2017

To me, something like
.ReturnsAsync()
would be more readable than
.Returns(Task.FromResult<object>(null))

@abatishchev
Copy link
Contributor

Here's what I've got in our shared tests project:

public static class MoqExtensions
{
	public static IReturnsResult<TMock> ReturnsAsync<TMock>(this IReturns<TMock, Task> mock) where TMock : class
	{
		return mock.Returns(Task.CompletedTask);
	}

	public static IReturnsResult<TMock> ReturnsEmptyAsync<TMock, TResult>(this IReturns<TMock, Task<IReadOnlyCollection<TResult>>> mock) where TMock : class
	{
		return mock.ReturnsAsync(new TResult[0]);
	}

	public static ISetupSequentialResult<Task<IReadOnlyCollection<TResult>>> ReturnsEmptyAsync<TResult>(this ISetupSequentialResult<Task<IReadOnlyCollection<TResult>>> setup)
	{
		return setup.Returns(Task.FromResult((IReadOnlyCollection<TResult>)new TResult[0]));
	}
}

@kzu Worth a pull request?

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

4 participants