Skip to content

Commit

Permalink
Merge pull request #261 from abatishchev/SetupSequence-ReturnsAsync-1
Browse files Browse the repository at this point in the history
Adding exension methods ReturnsAsync and ThrowsAsync for ISetupSequentialResult
  • Loading branch information
kzu committed May 18, 2016
2 parents 7c9e2a4 + fe053ce commit 978d379
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
29 changes: 24 additions & 5 deletions Source/SequenceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using Moq.Language.Flow;
using System.Threading.Tasks;
using Moq.Language;
using System.Diagnostics.CodeAnalysis;

namespace Moq
{
Expand All @@ -25,5 +22,27 @@ public static ISetupSequentialResult<TResult> SetupSequence<TMock, TResult>(
{
return new SetupSequentialContext<TMock, TResult>(mock, expression);
}

/// <summary>
/// Return a sequence of tasks, once per call.
/// </summary>
public static ISetupSequentialResult<Task<TResult>> ReturnsAsync<TResult>(this ISetupSequentialResult<Task<TResult>> setup, TResult value)
{
var tcs = new TaskCompletionSource<TResult>();
tcs.SetResult(value);

return setup.Returns(tcs.Task);
}

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

return setup.Returns(tcs.Task);
}
}
}
34 changes: 34 additions & 0 deletions UnitTests/SequenceExtensionsFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Xunit;

namespace Moq.Tests
Expand All @@ -20,6 +21,29 @@ public void PerformSequence()
Assert.Throws<InvalidOperationException>(() => mock.Object.Do());
}

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

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

Assert.Equal(2, mock.Object.DoAsync().Result);
Assert.Equal(3, mock.Object.DoAsync().Result);

try
{
var x = mock.Object.DoAsync().Result;
}
catch (AggregateException ex)
{
Assert.IsType<InvalidOperationException>(ex.GetBaseException());
}
}

[Fact]
public void PerformSequenceOnProperty()
{
Expand Down Expand Up @@ -67,7 +91,10 @@ public void PerformSequenceWithCallBase()
public interface IFoo
{
string Value { get; set; }

int Do();

Task<int> DoAsync();
}

public class Foo
Expand All @@ -76,6 +103,13 @@ public virtual string Do()
{
return "Ok";
}

public virtual Task<string> DoAsync()
{
var tcs = new TaskCompletionSource<string>();
tcs.SetResult("Ok");
return tcs.Task;
}
}
}
}

0 comments on commit 978d379

Please sign in to comment.