Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions docs/samples/tests/xunit/DisposeComponentsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public async Task ShouldCatchExceptionInDispose()
{
Render<ExceptionInDisposeComponent>();

Func<Task> act = () => DisposeComponentsAsync();

await Assert.ThrowsAsync<NotSupportedException>(act);
await DisposeComponentsAsync();
var exception = await Renderer.UnhandledException;
Assert.IsType<NotSupportedException>(exception);
}

[Fact]
Expand All @@ -38,7 +38,7 @@ public async Task ShouldCatchExceptionInDisposeAsync()
Render<ExceptionInDisposeAsyncComponent>();

await DisposeComponentsAsync();
var exception = Renderer.UnhandledException.Result;
var exception = await Renderer.UnhandledException;
Assert.IsType<NotSupportedException>(exception);
}

Expand Down
12 changes: 8 additions & 4 deletions docs/site/docs/interaction/dispose-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
**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)]
1 change: 0 additions & 1 deletion src/bunit/Rendering/BunitRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ public Task DisposeComponents()
});

rootComponents.Clear();
AssertNoUnhandledExceptions();
}

return returnTask;
Expand Down
8 changes: 5 additions & 3 deletions tests/bunit.tests/BunitContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ThrowExceptionComponent>();
Func<Task> action = () => DisposeComponentsAsync();

await DisposeComponentsAsync();

await action.ShouldThrowAsync<NotSupportedException>();
var actual = await Renderer.UnhandledException;
actual.ShouldBeAssignableTo<NotSupportedException>();
}

[Fact(DisplayName = "DisposeComponents disposes components nested in render fragments")]
Expand Down