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

Added method overloads to ISetupSequentialResult async void methods #1006

Merged
merged 1 commit into from
Apr 25, 2020
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## Unreleased

#### Added

* New SetupSequence verbs .PassAsync() and .ThrowsAsync(...) for async methods with void return type (@fuzzybair, #993)

## 4.14.0 (2020-04-24)

#### Added
Expand Down
42 changes: 42 additions & 0 deletions src/Moq/SequenceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ public static ISetupSequentialResult<ValueTask<TResult>> ReturnsAsync<TResult>(t
return setup.Returns(() => new ValueTask<TResult>(valueFunction()));
}

/// <summary>
/// Return a sequence of tasks, once per call.
/// </summary>
public static ISetupSequentialResult<Task> PassAsync(this ISetupSequentialResult<Task> setup)
{
return setup.Returns(() => Task.FromResult(0));
}

/// <summary>
/// Return a sequence of tasks, once per call.
/// </summary>
public static ISetupSequentialResult<ValueTask> PassAsync(this ISetupSequentialResult<ValueTask> setup)
{
return setup.Returns(() => new ValueTask());
}

/// <summary>
/// Throws a sequence of exceptions, once per call.
/// </summary>
Expand All @@ -72,5 +88,31 @@ public static ISetupSequentialResult<ValueTask<TResult>> ThrowsAsync<TResult>(th
return new ValueTask<TResult>(tcs.Task);
});
}

/// <summary>
/// Throws a sequence of exceptions, once per call.
/// </summary>
public static ISetupSequentialResult<Task> ThrowsAsync(this ISetupSequentialResult<Task> setup, Exception exception)
{
return setup.Returns(() =>
{
var tcs = new TaskCompletionSource<object>();
tcs.SetException(exception);
return tcs.Task;
});
}

/// <summary>
/// Throws a sequence of exceptions, once per call.
/// </summary>
public static ISetupSequentialResult<ValueTask> ThrowsAsync(this ISetupSequentialResult<ValueTask> setup, Exception exception)
{
return setup.Returns(() =>
{
var tcs = new TaskCompletionSource<object>();
tcs.SetException(exception);
return new ValueTask(tcs.Task);
});
}
}
}
59 changes: 50 additions & 9 deletions tests/Moq.Tests/SequenceExtensionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void PerformSequence()
}

[Fact]
public void PerformSequenceAsync()
public async Task PerformSequenceAsync()
{
var mock = new Mock<IFoo>();

Expand All @@ -40,15 +40,52 @@ public void PerformSequenceAsync()

Assert.Equal(2, mock.Object.DoAsync().Result);
Assert.Equal(3, mock.Object.DoAsync().Result);
await Assert.ThrowsAsync<InvalidOperationException>(async () => await mock.Object.DoAsync());
}

try
{
var x = mock.Object.DoAsync().Result;
}
catch (AggregateException ex)
{
Assert.IsType<InvalidOperationException>(ex.GetBaseException());
}
[Fact]
public async Task PerformSequenceValueTaskAsync()
{
var mock = new Mock<IFoo>();

mock.SetupSequence(x => x.DoValueAsync())
.ReturnsAsync(2)
.ReturnsAsync(() => 3)
.ThrowsAsync(new InvalidOperationException());

Assert.Equal(2, mock.Object.DoValueAsync().Result);
Assert.Equal(3, mock.Object.DoValueAsync().Result);
await Assert.ThrowsAsync<InvalidOperationException>(async () => await mock.Object.DoValueAsync());
}

[Fact]
public async Task PerformSequenceVoidAsync()
{
var mock = new Mock<IFoo>();

mock.SetupSequence(x => x.DoVoidAsync())
.PassAsync()
.PassAsync()
.ThrowsAsync(new InvalidOperationException());

await mock.Object.DoVoidAsync();
await mock.Object.DoVoidAsync();
await Assert.ThrowsAsync<InvalidOperationException>(async () => await mock.Object.DoVoidAsync());
}

[Fact]
public async Task PerformSequenceValueTaskVoidAsync()
{
var mock = new Mock<IFoo>();

mock.SetupSequence(x => x.DoValueVoidAsync())
.PassAsync()
.PassAsync()
.ThrowsAsync(new InvalidOperationException());

await mock.Object.DoValueVoidAsync();
await mock.Object.DoValueVoidAsync();
await Assert.ThrowsAsync<InvalidOperationException>(async () => await mock.Object.DoValueVoidAsync());
}

[Fact]
Expand Down Expand Up @@ -260,6 +297,10 @@ public interface IFoo

ValueTask<int> DoValueAsync();

Task DoVoidAsync();

ValueTask DoValueVoidAsync();

Func<int> GetFunc();

object GetObj();
Expand Down