Skip to content

Commit

Permalink
Merge pull request #60 from Blewzman/master
Browse files Browse the repository at this point in the history
Add extension methods for IReturns to allow specifying return values exceptions on async methods in a synchronous way
  • Loading branch information
kzu committed Nov 19, 2013
2 parents 7b880d6 + 468f958 commit 293a402
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Source/Language/IReturnsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Moq.Language.Flow
{
/// <summary>
/// Defines async extension methods on IReturns.
/// </summary>
public static class IReturnsExtensions
{
/// <summary>
/// Allows to specify the return value of an asynchronous method.
/// </summary>
public static IReturnsResult<TMock> ReturnsAsync<TMock, TResult>(this IReturns<TMock, Task<TResult>> mock, TResult value) where TMock : class
{
var tcs = new TaskCompletionSource<TResult>();
tcs.SetResult(value);

return mock.Returns(tcs.Task);
}

/// <summary>
/// Allows to specify the exception thrown by an asynchronous method.
/// </summary>
public static IReturnsResult<TMock> ThrowsAsync<TMock, TResult>(this IReturns<TMock, Task<TResult>> mock, Exception exception) where TMock : class
{
var tcs = new TaskCompletionSource<TResult>();
tcs.SetException(exception);

return mock.Returns(tcs.Task);
}
}
}
1 change: 1 addition & 0 deletions Source/Moq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>IReturns.tt</DependentUpon>
</Compile>
<Compile Include="Language\IReturnsExtensions.cs" />
<Compile Include="Language\ISetupSequentialResult.cs" />
<Compile Include="Language\ISetupConditionResult.cs" />
<Compile Include="Linq\FluentMockVisitor.cs" />
Expand Down
1 change: 1 addition & 0 deletions UnitTests/Moq.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Compile Include="ConditionalSetupFixture.cs" />
<Compile Include="CustomMatcherFixture.cs" />
<Compile Include="ExtensionsFixture.cs" />
<Compile Include="ReturnsExtensionsFixture.cs" />
<Compile Include="Linq\MockRepositoryQuerying.cs" />
<Compile Include="MockedDelegatesFixture.cs" />
<Compile Include="MockSequenceFixture.cs" />
Expand Down
176 changes: 176 additions & 0 deletions UnitTests/ReturnsExtensionsFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using System;
using System.Threading.Tasks;
using Moq.Language.Flow;
using Xunit;
using System.Collections.Generic;

namespace Moq.Tests
{
public class ReturnsExtensionsFixture
{
public interface IAsyncInterface
{
Task<string> NoParametersRefReturnType();

Task<int> NoParametersValueReturnType();

Task<string> RefParameterRefReturnType(string value);

Task<int> RefParameterValueReturnType(string value);

Task<string> ValueParameterRefReturnType(int value);

Task<int> ValueParameterValueReturnType(int value);
}

[Fact]
public void ReturnsAsync_on_NoParametersRefReturnType()
{
var mock = new Mock<IAsyncInterface>();
mock.Setup(x => x.NoParametersRefReturnType()).ReturnsAsync("TestString");

var task = mock.Object.NoParametersRefReturnType();

Assert.True(task.IsCompleted);
Assert.Equal("TestString", task.Result);
}

[Fact]
public void ReturnsAsync_on_NoParametersValueReturnType()
{
var mock = new Mock<IAsyncInterface>();
mock.Setup(x => x.NoParametersValueReturnType()).ReturnsAsync(36);

var task = mock.Object.NoParametersValueReturnType();

Assert.True(task.IsCompleted);
Assert.Equal(36, task.Result);
}

[Fact]
public void ReturnsAsync_on_RefParameterRefReturnType()
{
var mock = new Mock<IAsyncInterface>();
mock.Setup(x => x.RefParameterRefReturnType("Param1")).ReturnsAsync("TestString");

var task = mock.Object.RefParameterRefReturnType("Param1");

Assert.True(task.IsCompleted);
Assert.Equal("TestString", task.Result);
}

[Fact]
public void ReturnsAsync_on_RefParameterValueReturnType()
{
var mock = new Mock<IAsyncInterface>();
mock.Setup(x => x.RefParameterValueReturnType("Param1")).ReturnsAsync(36);

var task = mock.Object.RefParameterValueReturnType("Param1");

Assert.True(task.IsCompleted);
Assert.Equal(36, task.Result);
}

[Fact]
public void ReturnsAsync_on_ValueParameterRefReturnType()
{
var mock = new Mock<IAsyncInterface>();
mock.Setup(x => x.ValueParameterRefReturnType(36)).ReturnsAsync("TestString");

var task = mock.Object.ValueParameterRefReturnType(36);

Assert.True(task.IsCompleted);
Assert.Equal("TestString", task.Result);
}

[Fact]
public void ReturnsAsync_on_ValueParameterValueReturnType()
{
var mock = new Mock<IAsyncInterface>();
mock.Setup(x => x.ValueParameterValueReturnType(36)).ReturnsAsync(37);

var task = mock.Object.ValueParameterValueReturnType(36);

Assert.True(task.IsCompleted);
Assert.Equal(37, task.Result);
}

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

var task = mock.Object.NoParametersRefReturnType();

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

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

var task = mock.Object.NoParametersValueReturnType();

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

[Fact]
public void ThrowsAsync_on_RefParameterRefReturnType()
{
var mock = new Mock<IAsyncInterface>();
var exception = new InvalidOperationException();
mock.Setup(x => x.RefParameterRefReturnType("Param1")).ThrowsAsync(exception);

var task = mock.Object.RefParameterRefReturnType("Param1");

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

[Fact]
public void ThrowsAsync_on_RefParameterValueReturnType()
{
var mock = new Mock<IAsyncInterface>();
var exception = new InvalidOperationException();
mock.Setup(x => x.RefParameterValueReturnType("Param1")).ThrowsAsync(exception);

var task = mock.Object.RefParameterValueReturnType("Param1");

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

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

var task = mock.Object.ValueParameterRefReturnType(36);

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

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

var task = mock.Object.ValueParameterValueReturnType(36);

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

0 comments on commit 293a402

Please sign in to comment.