From ca1c77870ac4fdaff1f74f05011e6f5e11538286 Mon Sep 17 00:00:00 2001 From: Valentina Petrova Date: Thu, 31 Jul 2025 18:17:37 +0300 Subject: [PATCH 1/4] fix: add automocking examples to ReturnsAsync page - add new paragraph for Automocking - add 2 examples closes #787 --- basic-usage/mock/returns-async.md | 141 ++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) diff --git a/basic-usage/mock/returns-async.md b/basic-usage/mock/returns-async.md index 0a02ac3..cfcacc3 100644 --- a/basic-usage/mock/returns-async.md +++ b/basic-usage/mock/returns-async.md @@ -214,6 +214,147 @@ Another common case is to mock an async method call to execute custom logic befo End Function {{endregion}} +## Return a Value for an Async Method from a Mocking Container (Automocking) + +In scenarios where a class has dependencies involving asynchronous methods, a [Mocking Container]({%slug justmock/basic-usage/automocking%}) can be created from the class. The `ReturnsAsync` method can then be used to provide a custom return value for the asynchronous method. + +To take a look at this scenario, we will use the following code: + +#### __[C#]__ +{{region ReturnAsync#TaskClient}} + + public interface ITaskAsync + { + Task GenericTaskWithValueReturnType(); + Task GenericTaskWithValueReturnTypeAndOneParam(int value); + Task GenericTaskWithObjectReturnType(); + Task GenericTaskWithObjectReturnTypeAndOneParam(object value); + } + + public class TaskClient + { + private ITaskAsync task; + + public TaskClient(ITaskAsync t) + { + task = t; + } + + public async Task TaskUsageWithValue() + { + return await task.GenericTaskWithValueReturnTypeAndOneParam(10); + } + } +{{endregion}} + +#### __[VB]__ +{{region ReturnAsync#TaskClient}} + + Public Interface ITaskAsync + Function GenericTaskWithValueReturnType() As Task(Of Integer) + Function GenericTaskWithValueReturnTypeAndOneParam(value As Integer) As Task(Of Integer) + Function GenericTaskWithObjectReturnType() As Task(Of Object) + Function GenericTaskWithObjectReturnTypeAndOneParam(value As Object) As Task(Of Object) + End Interface + + Public Class TaskClient + Private task As ITaskAsync + + Public Sub New(t As ITaskAsync) + task = t + End Sub + + Public Async Function TaskUsageWithValue() As Task(Of Integer) + Return Await task.GenericTaskWithValueReturnTypeAndOneParam(10) + End Function + End Class +{{endregion}} + +In **Example 5**, we demonstrate the basic usage of ReturnsAsync with MockingContainer using type inference. + +#### __[C#] Example 5: Using ReturnsAsync with MockingContainer__ + +{{region ReturnAsync#TestTaskAutomockingReturnsAsyncResult}} + + [TestMethod] + public async Task TestTaskAutomockingReturnsAsyncResult() + { + // Arange + var container = new MockingContainer(); + var expectedResult = 10; + + container.Arrange(t => t.GenericTaskWithValueReturnTypeAndOneParam(Arg.AnyInt)).ReturnsAsync(expectedResult); + + // Act + var actualResult = await container.Instance.TaskUsageWithValue(); + + // Assert + Assert.Equals(expectedResult, actualResult); + } +{{endregion}} + +#### __[VB] Example 5: Using ReturnsAsync with MockingContainer__ +{{region ReturnAsync#TestTaskAutomockingReturnsAsyncResult}} + + + Public Async Function TestTaskAutomockingReturnsAsyncResult() As Task + ' Arange + Dim container = New MockingContainer(Of TaskClient)() + Dim expectedResult = 10 + + container.Arrange(Of ITaskAsync)(Function(t) t.GenericTaskWithValueReturnTypeAndOneParam(Arg.AnyInt)).ReturnsAsync(expectedResult) + + ' Act + Dim actualResult = Await container.Instance.TaskUsageWithValue() + + ' Assert + Assert.Equals(expectedResult, actualResult) + End Function +{{endregion}} + +In **Example 6**, we extend this by explicitly specifying the return type, showing a more controlled and type-safe approach. + +### __[C#] Example 6: Extending ReturnsAsync with Explicit Return Type in MockingContainer__ + +{{region ReturnAsync#TestTaskAutomockingAsyncResultWithIntParameter}} + + [TestMethod] + public async Task TestTaskAutomockingAsyncResultWithIntParameter() + { + // Arange + var container = new MockingContainer(); + var expectedResult = 10; + + container.Arrange(t => t.GenericTaskWithValueReturnTypeAndOneParam(Arg.AnyInt)).ReturnsAsync(expectedResult); + + // Act + var actualResult = await container.Instance.TaskUsageWithValue(); + + // Assert + Assert.Equals(expectedResult, actualResult); + } +{{endregion}} + +### __[VB] Example 6: Extending ReturnsAsync with Explicit Return Type in MockingContainer__ + +{{region ReturnAsync#TestTaskAutomockingAsyncResultWithIntParameter}} + + [TestMethod] + + Public Async Function TestTaskAutomockingAsyncResultWithIntParameter() As Task + ' Arange + Dim container = New MockingContainer(Of TaskClient)() + Dim expectedResult = 10 + + container.Arrange(Of ITaskAsync, Integer)(Function(t) t.GenericTaskWithValueReturnTypeAndOneParam(Arg.AnyInt)).ReturnsAsync(expectedResult) + + ' Act + Dim actualResult = Await container.Instance.TaskUsageWithValue() + + ' Assert + Assert.Equals(expectedResult, actualResult) + End Function +{{endregion}} ## Testing Exception Handling for Async Methods From 16f27ae02f3ba561435962348178300973220c2a Mon Sep 17 00:00:00 2001 From: Valentina Petrova Date: Thu, 31 Jul 2025 18:17:37 +0300 Subject: [PATCH 2/4] fix: add automocking examples to ReturnsAsync page - add new paragraph for Automocking - add 2 examples closes #787 From 804c54301c87bfbf82c290569a64f06ee81f98a8 Mon Sep 17 00:00:00 2001 From: Valentina Petrova Date: Fri, 1 Aug 2025 14:42:37 +0300 Subject: [PATCH 3/4] fix: add automocking examples to ReturnsAsync page - add new paragraph for Automocking - add 2 examples closes #787 --- basic-usage/mock/returns-async.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/basic-usage/mock/returns-async.md b/basic-usage/mock/returns-async.md index cfcacc3..77bb5cc 100644 --- a/basic-usage/mock/returns-async.md +++ b/basic-usage/mock/returns-async.md @@ -216,7 +216,7 @@ Another common case is to mock an async method call to execute custom logic befo ## Return a Value for an Async Method from a Mocking Container (Automocking) -In scenarios where a class has dependencies involving asynchronous methods, a [Mocking Container]({%slug justmock/basic-usage/automocking%}) can be created from the class. The `ReturnsAsync` method can then be used to provide a custom return value for the asynchronous method. +In scenarios where a class has dependencies involving asynchronous methods, it is possible to create a [Mocking Container]({%slug justmock/basic-usage/automocking%}). The `ReturnsAsync` method can then be used to provide a custom return value for the asynchronous method. To take a look at this scenario, we will use the following code: @@ -225,10 +225,7 @@ To take a look at this scenario, we will use the following code: public interface ITaskAsync { - Task GenericTaskWithValueReturnType(); Task GenericTaskWithValueReturnTypeAndOneParam(int value); - Task GenericTaskWithObjectReturnType(); - Task GenericTaskWithObjectReturnTypeAndOneParam(object value); } public class TaskClient @@ -251,10 +248,7 @@ To take a look at this scenario, we will use the following code: {{region ReturnAsync#TaskClient}} Public Interface ITaskAsync - Function GenericTaskWithValueReturnType() As Task(Of Integer) Function GenericTaskWithValueReturnTypeAndOneParam(value As Integer) As Task(Of Integer) - Function GenericTaskWithObjectReturnType() As Task(Of Object) - Function GenericTaskWithObjectReturnTypeAndOneParam(value As Object) As Task(Of Object) End Interface Public Class TaskClient From 1bc7183f33b81bda507783486166de009578bd76 Mon Sep 17 00:00:00 2001 From: Valentina Petrova Date: Fri, 1 Aug 2025 14:42:37 +0300 Subject: [PATCH 4/4] fix: add automocking examples to ReturnsAsync page - add new paragraph for Automocking - add 2 examples closes #787 --- basic-usage/mock/returns-async.md | 1 - 1 file changed, 1 deletion(-) diff --git a/basic-usage/mock/returns-async.md b/basic-usage/mock/returns-async.md index 77bb5cc..85ea772 100644 --- a/basic-usage/mock/returns-async.md +++ b/basic-usage/mock/returns-async.md @@ -333,7 +333,6 @@ In **Example 6**, we extend this by explicitly specifying the return type, showi {{region ReturnAsync#TestTaskAutomockingAsyncResultWithIntParameter}} - [TestMethod] Public Async Function TestTaskAutomockingAsyncResultWithIntParameter() As Task ' Arange