You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/site/docs/interaction/awaiting-async-state.md
+39-1Lines changed: 39 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,4 +3,42 @@ uid: awaiting-async-state
3
3
title: Awaiting an Asynchronous State Change in a Component Under Test
4
4
---
5
5
6
-
# Awaiting an Asynchronous State Change
6
+
# Awaiting an Asynchronous State Change
7
+
8
+
A test can fail if a component performs asynchronous renders, e.g. because it was awaiting an task to complete before continuing its render life-cycle. For example, if a component is waiting for a async web service to return data to it in the `OnInitializedAsync()` life-cycle method, before rendering it to the render tree.
9
+
10
+
This happens because tests execute in the test framework's synchronization context and the test renderer executes renders in its own synchronization context.
11
+
12
+
bUnit comes with two methods that helps deal with this issue, the [`WaitForState(Func<Boolean>, TimeSpan?)`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForState(Bunit.IRenderedFragmentBase,System.Func{System.Boolean},System.Nullable{System.TimeSpan})) method covered on this page, and the [`WaitForAssertion(Action, TimeSpan?)`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForAssertion(Bunit.IRenderedFragmentBase,System.Action,System.Nullable{System.TimeSpan})) method covered on the <xref:async-assertion> page.
13
+
14
+
## Waiting for state using `WaitForState`
15
+
16
+
The [`WaitForState(Func<Boolean>, TimeSpan?)`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForState(Bunit.IRenderedFragmentBase,System.Func{System.Boolean},System.Nullable{System.TimeSpan})) method can be used to block and wait in a test method, until the provided predicate returns true, or the timeout is reached (the default timeout is one second).
17
+
18
+
Let us look at an example. Consider the following `<AsyncData>` component, who awaits an async `TextService` in its `OnInitializedAsync()` life-cycle method. When the service returns the data, the component will automatically re-render, to update its rendered markup.
1. The test uses a `TaskCompletionSource<string>` to simulate an async web service.
29
+
2. In the second highlighted line, the result is provided to the component through the `textService`. This causes the component to re-render.
30
+
3. In the third highlighted line, the `WaitForState()` method is used to block the test until the predicate provided to it returns true.
31
+
4. Finally, the tests assertion step can execute, knowing that the desired state has been reached.
32
+
33
+
> [!NOTE]
34
+
> The wait predicate and an assertion should not verify the same thing. Instead, use the [`WaitForAssertion(...)`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForAssertion(Bunit.IRenderedFragmentBase,System.Action,System.Nullable{System.TimeSpan})) method covered on the <xref:async-assertion> page instead.
35
+
36
+
### Controlling wait timeout
37
+
38
+
The timeout, which defaults to one second, can be controlled by passing a `TimeSpan` as the second argument to the `WaitForState()` method, e.g.:
0 commit comments