Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
65 changes: 65 additions & 0 deletions Telerik.JustMock.Tests/ReturnAsyncFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
using Telerik.JustMock.Helpers;
using System.Threading.Tasks;
using System.Security.Policy;
using Telerik.JustMock.AutoMock;


#region JustMock Test Attributes
Expand Down Expand Up @@ -61,6 +62,22 @@ public interface ITaskAsync
Task<object> GenericTaskWithObjectReturnTypeAndOneParam(object value);
}

public class TaskClient
{
private ITaskAsync task;

public TaskClient(ITaskAsync t)
{
task = t;
}

public async Task<int> TaskUsageWithValue()
{
return await task.GenericTaskWithValueReturnTypeAndOneParam(10);
}
}


#if NETCORE
public interface IValueTaskAsync
{
Expand Down Expand Up @@ -158,6 +175,38 @@ public async Task ShouldReturnAsyncValueForGenericTaskWithObjectReturnTypeAndOne
Assert.Same(expected, result);
}

[TestMethod, TestCategory("Lite"), TestCategory("ReturnsAsync")]
public async Task TestTaskAutomockingReturnsAsyncResult()
{
// Arange
var container = new MockingContainer<TaskClient>();
var expectedResult = 10;

container.Arrange<ITaskAsync>(t => t.GenericTaskWithValueReturnTypeAndOneParam(Arg.AnyInt)).ReturnsAsync(expectedResult);

// Act
var actualResult = await container.Instance.TaskUsageWithValue();

// Assert
Assert.Equals(expectedResult, actualResult);
}

[TestMethod, TestCategory("Lite"), TestCategory("ReturnsAsync")]
public async Task TestTaskAutomockingAsyncResultWithIntParameter()
{
// Arange
var container = new MockingContainer<TaskClient>();
var expectedResult = 10;

container.Arrange<ITaskAsync, int>(t => t.GenericTaskWithValueReturnTypeAndOneParam(Arg.AnyInt)).ReturnsAsync(expectedResult);

// Act
var actualResult = await container.Instance.TaskUsageWithValue();

// Assert
Assert.Equals(expectedResult, actualResult);
}

#if NETCORE
[TestMethod, TestCategory("Lite"), TestCategory("ReturnsAsync")]
public async Task ShouldReturnAsyncValueForGenericValueTaskWithinAsyncTest()
Expand Down Expand Up @@ -228,6 +277,22 @@ public async Task ShouldReturnAsyncValueForGenericValueTaskWithObjectReturnTypeA

Assert.Same(expected, result);
}

[TestMethod, TestCategory("Lite"), TestCategory("ReturnsAsync")]
public async Task TestValueTaskAutomockingAsyncResultWithIntParameter()
{
// Arange
var container = new MockingContainer<TaskClient>();
var expectedResult = 10;

container.Arrange<IValueTaskAsync, int>(t => t.GenericTaskWithValueReturnTypeAndOneParam(Arg.AnyInt)).ReturnsAsync(expectedResult);

// Act
var actualResult = await container.Instance.TaskUsageWithValue();

// Assert
Assert.Equals(expectedResult, actualResult);
}
#endif
}
}
78 changes: 61 additions & 17 deletions Telerik.JustMock/AutoMock/MockingContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using Telerik.JustMock.AutoMock.Ninject;
using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings.Resolvers;
using Telerik.JustMock.AutoMock.Ninject.Syntax;
Expand Down Expand Up @@ -183,32 +184,75 @@ public FuncExpectation<object> Arrange<TInterface>(Expression<Func<TInterface, o
return ProfilerInterceptor.GuardInternal(() => this.Get<TInterface>().Arrange(expression));
}

/// <summary>
/// <summary>
/// Entry-point for setting expectations.
/// </summary>
/// <typeparam name="TInterface">Mocking interface</typeparam>
/// <param name="expression">Target expression</param>
/// <returns>
/// Reference to <see cref="FuncExpectation<Task>"/> to setup the mock.
/// </returns>
public FuncExpectation<Task> Arrange<TInterface>(Expression<Func<TInterface, Task>> expression)
{
return ProfilerInterceptor.GuardInternal(() => this.Get<TInterface>().Arrange(expression));
}

/// <summary>
/// Entry-point for setting expectations.
/// </summary>
/// <typeparam name="TInterface">
/// Mocking interface
/// </typeparam>
/// <typeparam name="TInterface">Mocking interface</typeparam>
/// <typeparam name="TResult">Task result type</typeparam>
Comment thread
MariaDch marked this conversation as resolved.
/// <param name="expression">Target expression</param>
/// <returns>
/// Reference to <see cref="ActionExpectation"/> to setup the mock.
/// Reference to <see cref="FuncExpectation<Task<TResult>>"/> to setup the mock.
/// </returns>
public ActionExpectation Arrange<TInterface>(Expression<Action<TInterface>> expression)
public FuncExpectation<Task<TResult>> Arrange<TInterface, TResult>(Expression<Func<TInterface, Task<TResult>>> expression)
{
return ProfilerInterceptor.GuardInternal(() => this.Get<TInterface>().Arrange(expression));
}

#if NETCORE
/// <summary>
/// Entry-point for setting expectations.
/// </summary>
/// <typeparam name="TInterface">Mocking interface</typeparam>
/// <typeparam name="TResult">Task result type</typeparam>
/// <param name="expression">Target expression</param>
/// <returns>
/// Reference to <see cref="FuncExpectation<ValueTask<TResult>>"/> to setup the mock.
/// </returns>
public FuncExpectation<ValueTask<TResult>> Arrange<TInterface, TResult>(Expression<Func<TInterface, ValueTask<TResult>>> expression)
{
return ProfilerInterceptor.GuardInternal(() => this.Get<TInterface>().Arrange(expression));
}
#endif

/// <summary>
/// Entry-point for setting expectations.
/// </summary>
/// <typeparam name="TInterface">
/// Mocking interface
/// </typeparam>
/// <param name="expression">Target expression</param>
/// <returns>
/// Reference to <see cref="ActionExpectation"/> to setup the mock.
/// </returns>
public ActionExpectation Arrange<TInterface>(Expression<Action<TInterface>> expression)
{
return ProfilerInterceptor.GuardInternal(() => this.Get<TInterface>().Arrange(expression));
}

/// <summary>
/// Entry-point for setting expectations.
/// </summary>
/// <typeparam name="TInterface">
/// Mocking interface
/// </typeparam>
/// <param name="action">Target action</param>
/// <returns>
/// Reference to <see cref="ActionExpectation"/> to setup the mock.
/// </returns>
public ActionExpectation ArrangeSet<TInterface>(Action<TInterface> action)
/// <summary>
/// Entry-point for setting expectations.
/// </summary>
/// <typeparam name="TInterface">
/// Mocking interface
/// </typeparam>
/// <param name="action">Target action</param>
/// <returns>
/// Reference to <see cref="ActionExpectation"/> to setup the mock.
/// </returns>
public ActionExpectation ArrangeSet<TInterface>(Action<TInterface> action)
{
return ProfilerInterceptor.GuardInternal(() => this.Get<TInterface>().ArrangeSet(action));
}
Expand Down
44 changes: 39 additions & 5 deletions Telerik.JustMock/Expectations/AsyncExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Telerik.JustMock
public static partial class AsyncExtensions
{
/// <summary>
/// Specifies the return value for a asynchronous method.
/// Specifies the return value for an asynchronous method.
/// </summary>
/// <typeparam name="TResult">Type of the return value.</typeparam>
/// <returns>Reference to <see cref="IAssertable"/> interface.</returns>
Expand All @@ -41,9 +41,23 @@ public static IAssertable ReturnsAsync<TResult>(this FuncExpectation<Task<TResul
return mock;
});
}

/// <summary>
/// Specifies a function to evaluate and return the value for a asynchronous method.
/// Specifies the return value for an asynchronous method.
/// </summary>
/// <typeparam name="TResult">Type of the return value.</typeparam>
/// <returns>Reference to <see cref="IAssertable"/> interface.</returns>
public static IAssertable ReturnsAsync<TResult>(this FuncExpectation<Task> mock, TResult value)
{
return ProfilerInterceptor.GuardInternal(() =>
{
mock.ReturnsAsync(() => value);
return mock;
});
}

/// <summary>
/// Specifies a function to evaluate and return the value for an asynchronous method.
/// </summary>
/// <typeparam name="TResult">Type of the return value.</typeparam>
/// <param name="valueFunction">The function that will evaluate the return value.</param>
Expand All @@ -61,9 +75,29 @@ public static IAssertable ReturnsAsync<TResult>(this FuncExpectation<Task<TResul
});
}


/// <summary>
/// Specifies a function to evaluate and return the value for an asynchronous method.
/// </summary>
/// <typeparam name="TResult">Type of the return value.</typeparam>
/// <param name="valueFunction">The function that will evaluate the return value.</param>
/// <returns>Reference to <see cref="IAssertable"/> interface.</returns>
public static IAssertable ReturnsAsync<TResult>(this FuncExpectation<Task> mock, Func<TResult> valueFunction)
{
return ProfilerInterceptor.GuardInternal(() =>
{
if (valueFunction == null)
{
return mock.ReturnsAsync(new Func<TResult>(() => default(TResult)));
}

return mock.Returns(() => Task.FromResult(valueFunction()));
});
}

#if NETCORE
/// <summary>
/// Specifies the return value for a asynchronous method.
/// Specifies the return value for an asynchronous method.
/// </summary>
/// <typeparam name="TResult">Type of the return value.</typeparam>
/// <returns>Reference to <see cref="IAssertable"/> interface.</returns>
Expand All @@ -77,7 +111,7 @@ public static IAssertable ReturnsAsync<TResult>(this FuncExpectation<ValueTask<T
}

/// <summary>
/// Specifies a function to evaluate and return the value for a asynchronous method.
/// Specifies a function to evaluate and return the value for an asynchronous method.
/// </summary>
/// <typeparam name="TResult">Type of the return value.</typeparam>
/// <param name="valueFunction">The function that will evaluate the return value.</param>
Expand Down