Skip to content

Commit 9506afd

Browse files
fix: add Automocking examples to ReturnsAsync article (#147)
* fix: add automocking examples to ReturnsAsync page - add new paragraph for Automocking - add 2 examples closes #787
1 parent 6808dbe commit 9506afd

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

basic-usage/mock/returns-async.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,140 @@ Another common case is to mock an async method call to execute custom logic befo
214214
End Function
215215
{{endregion}}
216216

217+
## Return a Value for an Async Method from a Mocking Container (Automocking)
218+
219+
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.
220+
221+
To take a look at this scenario, we will use the following code:
222+
223+
#### __[C#]__
224+
{{region ReturnAsync#TaskClient}}
225+
226+
public interface ITaskAsync
227+
{
228+
Task<int> GenericTaskWithValueReturnTypeAndOneParam(int value);
229+
}
230+
231+
public class TaskClient
232+
{
233+
private ITaskAsync task;
234+
235+
public TaskClient(ITaskAsync t)
236+
{
237+
task = t;
238+
}
239+
240+
public async Task<int> TaskUsageWithValue()
241+
{
242+
return await task.GenericTaskWithValueReturnTypeAndOneParam(10);
243+
}
244+
}
245+
{{endregion}}
246+
247+
#### __[VB]__
248+
{{region ReturnAsync#TaskClient}}
249+
250+
Public Interface ITaskAsync
251+
Function GenericTaskWithValueReturnTypeAndOneParam(value As Integer) As Task(Of Integer)
252+
End Interface
253+
254+
Public Class TaskClient
255+
Private task As ITaskAsync
256+
257+
Public Sub New(t As ITaskAsync)
258+
task = t
259+
End Sub
260+
261+
Public Async Function TaskUsageWithValue() As Task(Of Integer)
262+
Return Await task.GenericTaskWithValueReturnTypeAndOneParam(10)
263+
End Function
264+
End Class
265+
{{endregion}}
266+
267+
In **Example 5**, we demonstrate the basic usage of ReturnsAsync with MockingContainer using type inference.
268+
269+
#### __[C#] Example 5: Using ReturnsAsync with MockingContainer__
270+
271+
{{region ReturnAsync#TestTaskAutomockingReturnsAsyncResult}}
272+
273+
[TestMethod]
274+
public async Task TestTaskAutomockingReturnsAsyncResult()
275+
{
276+
// Arange
277+
var container = new MockingContainer<TaskClient>();
278+
var expectedResult = 10;
279+
280+
container.Arrange<ITaskAsync>(t => t.GenericTaskWithValueReturnTypeAndOneParam(Arg.AnyInt)).ReturnsAsync(expectedResult);
281+
282+
// Act
283+
var actualResult = await container.Instance.TaskUsageWithValue();
284+
285+
// Assert
286+
Assert.Equals(expectedResult, actualResult);
287+
}
288+
{{endregion}}
289+
290+
#### __[VB] Example 5: Using ReturnsAsync with MockingContainer__
291+
{{region ReturnAsync#TestTaskAutomockingReturnsAsyncResult}}
292+
293+
<TestMethod>
294+
Public Async Function TestTaskAutomockingReturnsAsyncResult() As Task
295+
' Arange
296+
Dim container = New MockingContainer(Of TaskClient)()
297+
Dim expectedResult = 10
298+
299+
container.Arrange(Of ITaskAsync)(Function(t) t.GenericTaskWithValueReturnTypeAndOneParam(Arg.AnyInt)).ReturnsAsync(expectedResult)
300+
301+
' Act
302+
Dim actualResult = Await container.Instance.TaskUsageWithValue()
303+
304+
' Assert
305+
Assert.Equals(expectedResult, actualResult)
306+
End Function
307+
{{endregion}}
308+
309+
In **Example 6**, we extend this by explicitly specifying the return type, showing a more controlled and type-safe approach.
310+
311+
### __[C#] Example 6: Extending ReturnsAsync with Explicit Return Type in MockingContainer__
312+
313+
{{region ReturnAsync#TestTaskAutomockingAsyncResultWithIntParameter}}
314+
315+
[TestMethod]
316+
public async Task TestTaskAutomockingAsyncResultWithIntParameter()
317+
{
318+
// Arange
319+
var container = new MockingContainer<TaskClient>();
320+
var expectedResult = 10;
321+
322+
container.Arrange<ITaskAsync, int>(t => t.GenericTaskWithValueReturnTypeAndOneParam(Arg.AnyInt)).ReturnsAsync(expectedResult);
323+
324+
// Act
325+
var actualResult = await container.Instance.TaskUsageWithValue();
326+
327+
// Assert
328+
Assert.Equals(expectedResult, actualResult);
329+
}
330+
{{endregion}}
331+
332+
### __[VB] Example 6: Extending ReturnsAsync with Explicit Return Type in MockingContainer__
333+
334+
{{region ReturnAsync#TestTaskAutomockingAsyncResultWithIntParameter}}
335+
336+
<TestMethod>
337+
Public Async Function TestTaskAutomockingAsyncResultWithIntParameter() As Task
338+
' Arange
339+
Dim container = New MockingContainer(Of TaskClient)()
340+
Dim expectedResult = 10
341+
342+
container.Arrange(Of ITaskAsync, Integer)(Function(t) t.GenericTaskWithValueReturnTypeAndOneParam(Arg.AnyInt)).ReturnsAsync(expectedResult)
343+
344+
' Act
345+
Dim actualResult = Await container.Instance.TaskUsageWithValue()
346+
347+
' Assert
348+
Assert.Equals(expectedResult, actualResult)
349+
End Function
350+
{{endregion}}
217351

218352
## Testing Exception Handling for Async Methods
219353

0 commit comments

Comments
 (0)