diff --git a/docs/samples/tests/xunit/DisposeComponentsTest.cs b/docs/samples/tests/xunit/DisposeComponentsTest.cs index bf651ff94..be0b1837d 100644 --- a/docs/samples/tests/xunit/DisposeComponentsTest.cs +++ b/docs/samples/tests/xunit/DisposeComponentsTest.cs @@ -27,9 +27,9 @@ public async Task ShouldCatchExceptionInDispose() { Render(); - Func act = () => DisposeComponentsAsync(); - - await Assert.ThrowsAsync(act); + await DisposeComponentsAsync(); + var exception = await Renderer.UnhandledException; + Assert.IsType(exception); } [Fact] @@ -38,7 +38,7 @@ public async Task ShouldCatchExceptionInDisposeAsync() Render(); await DisposeComponentsAsync(); - var exception = Renderer.UnhandledException.Result; + var exception = await Renderer.UnhandledException; Assert.IsType(exception); } diff --git a/docs/site/docs/interaction/dispose-components.md b/docs/site/docs/interaction/dispose-components.md index 6b22fb465..9781d884b 100644 --- a/docs/site/docs/interaction/dispose-components.md +++ b/docs/site/docs/interaction/dispose-components.md @@ -27,10 +27,14 @@ _ = DisposeComponentsAsync(); ``` ## Checking for exceptions -`Dispose` as well as `DisposeAsync` can throw exceptions which can be asserted as well. If a component under test throws an exception in `Dispose` the [`DisposeComponents`](xref:Bunit.BunitContext.DisposeComponentsAsync) will throw the exception to the user code: +Both `Dispose` and `DisposeAsync` can throw exceptions, which can be asserted during tests. When a component under test throws an exception in either `Dispose` or `DisposeAsync`, the exception is caught by the renderer and made available via the `Renderer.UnhandledException` task. The `DisposeComponentsAsync` method itself will not throw the exception. -[!code-csharp[DisposeComponentsTest.cs](../../../samples/tests/xunit/DisposeComponentsTest.cs#L28-L32)] +This allows for consistent testing of exceptions during disposal, regardless of whether the disposal is synchronous or asynchronous. -`DisposeAsync` behaves a bit different. The following example will demonstrate how to assert an exception in `DisposeAsync`: +The following examples demonstrate how to assert that an exception was thrown during disposal: -[!code-csharp[DisposeComponentsTest.cs](../../../samples/tests/xunit/DisposeComponentsTest.cs#L38-L42)] \ No newline at end of file +**Asserting exception in `Dispose`:** +[!code-csharp[DisposeComponentsTest.cs](../../../samples/tests/xunit/DisposeComponentsTest.cs#L24-L32)] + +**Asserting exception in `DisposeAsync`:** +[!code-csharp[DisposeComponentsTest.cs](../../../samples/tests/xunit/DisposeComponentsTest.cs#L34-L42)] \ No newline at end of file diff --git a/src/bunit/Rendering/BunitRenderer.cs b/src/bunit/Rendering/BunitRenderer.cs index 646c008a9..23a9afeca 100644 --- a/src/bunit/Rendering/BunitRenderer.cs +++ b/src/bunit/Rendering/BunitRenderer.cs @@ -235,7 +235,6 @@ public Task DisposeComponents() }); rootComponents.Clear(); - AssertNoUnhandledExceptions(); } return returnTask; diff --git a/tests/bunit.tests/BunitContextTest.cs b/tests/bunit.tests/BunitContextTest.cs index 2d7cd144e..cdbc74601 100644 --- a/tests/bunit.tests/BunitContextTest.cs +++ b/tests/bunit.tests/BunitContextTest.cs @@ -30,13 +30,15 @@ public async Task Test102() callStack.Count.ShouldBe(2); } - [Fact(DisplayName = "DisposeComponents rethrows exceptions from Dispose methods in components")] + [Fact(DisplayName = "DisposeComponents captures exceptions from Dispose in Renderer.UnhandledException")] public async Task Test103() { Render(); - Func action = () => DisposeComponentsAsync(); + + await DisposeComponentsAsync(); - await action.ShouldThrowAsync(); + var actual = await Renderer.UnhandledException; + actual.ShouldBeAssignableTo(); } [Fact(DisplayName = "DisposeComponents disposes components nested in render fragments")]