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

Add ThrowsAsync overload for non-generic Task #357

Merged
merged 1 commit into from
Jun 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Source/ReturnsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ public static class ReturnsExtensions
return mock.Returns(() => Task.FromResult(valueFunction()));
}

/// <summary>
/// Specifies the exception to throw when the asynchronous method is invoked.
/// </summary>
/// <typeparam name="TMock">Mocked type.</typeparam>
/// <param name="mock">Returns verb which represents the mocked type and the task return type</param>
/// <param name="exception">Exception instance to throw.</param>
public static IReturnsResult<TMock> ThrowsAsync<TMock>(this IReturns<TMock, Task> mock, Exception exception) where TMock : class
{
var tcs = new TaskCompletionSource<bool>();
tcs.SetException(exception);

return mock.Returns(tcs.Task);
}

/// <summary>
/// Specifies the exception to throw when the asynchronous method is invoked.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions UnitTests/ReturnsExtensionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class ReturnsExtensionsFixture
{
public interface IAsyncInterface
{
Task NoParametersNonGenericTaskReturnType();

Task<string> NoParametersRefReturnType();

Task<int> NoParametersValueReturnType();
Expand Down Expand Up @@ -192,6 +194,19 @@ public void ReturnsAsyncFunc_onEachInvocation_RefReturnTypeLazyEvaluation()
Assert.NotSame(firstEvaluationResult, secondEvaluationResult);
}

[Fact]
public void ThrowsAsync_on_NoParametersNonGenericTaskReturnType()
{
var mock = new Mock<IAsyncInterface>();
var exception = new InvalidOperationException();
mock.Setup(x => x.NoParametersNonGenericTaskReturnType()).ThrowsAsync(exception);

var task = mock.Object.NoParametersNonGenericTaskReturnType();

Assert.True(task.IsFaulted);
Assert.Equal(exception, task.Exception.InnerException);
}

[Fact]
public void ThrowsAsync_on_NoParametersRefReturnType()
{
Expand Down