Skip to content

Commit 7f99c27

Browse files
committed
fix: Don't rethrow in DiposeComponents directly
1 parent a517610 commit 7f99c27

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

docs/samples/tests/xunit/DisposeComponentsTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public async Task ShouldCatchExceptionInDispose()
2727
{
2828
Render<ExceptionInDisposeComponent>();
2929

30-
Func<Task> act = () => DisposeComponentsAsync();
31-
32-
await Assert.ThrowsAsync<NotSupportedException>(act);
30+
await DisposeComponentsAsync();
31+
var exception = await Renderer.UnhandledException;
32+
Assert.IsType<NotSupportedException>(exception);
3333
}
3434

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

4040
await DisposeComponentsAsync();
41-
var exception = Renderer.UnhandledException.Result;
41+
var exception = await Renderer.UnhandledException;
4242
Assert.IsType<NotSupportedException>(exception);
4343
}
4444

docs/site/docs/interaction/dispose-components.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ _ = DisposeComponentsAsync();
2727
```
2828

2929
## Checking for exceptions
30-
`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:
30+
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.
3131

32-
[!code-csharp[DisposeComponentsTest.cs](../../../samples/tests/xunit/DisposeComponentsTest.cs#L28-L32)]
32+
This allows for consistent testing of exceptions during disposal, regardless of whether the disposal is synchronous or asynchronous.
3333

34-
`DisposeAsync` behaves a bit different. The following example will demonstrate how to assert an exception in `DisposeAsync`:
34+
The following examples demonstrate how to assert that an exception was thrown during disposal:
3535

36-
[!code-csharp[DisposeComponentsTest.cs](../../../samples/tests/xunit/DisposeComponentsTest.cs#L38-L42)]
36+
**Asserting exception in `Dispose`:**
37+
[!code-csharp[DisposeComponentsTest.cs](../../../samples/tests/xunit/DisposeComponentsTest.cs#L24-L32)]
38+
39+
**Asserting exception in `DisposeAsync`:**
40+
[!code-csharp[DisposeComponentsTest.cs](../../../samples/tests/xunit/DisposeComponentsTest.cs#L34-L42)]

src/bunit/Rendering/BunitRenderer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ public Task DisposeComponents()
235235
});
236236

237237
rootComponents.Clear();
238-
AssertNoUnhandledExceptions();
239238
}
240239

241240
return returnTask;

tests/bunit.tests/BunitContextTest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ public async Task Test102()
3030
callStack.Count.ShouldBe(2);
3131
}
3232

33-
[Fact(DisplayName = "DisposeComponents rethrows exceptions from Dispose methods in components")]
33+
[Fact(DisplayName = "DisposeComponents captures exceptions from Dispose in Renderer.UnhandledException")]
3434
public async Task Test103()
3535
{
3636
Render<ThrowExceptionComponent>();
37-
Func<Task> action = () => DisposeComponentsAsync();
37+
38+
await DisposeComponentsAsync();
3839

39-
await action.ShouldThrowAsync<NotSupportedException>();
40+
var actual = await Renderer.UnhandledException;
41+
actual.ShouldBeAssignableTo<NotSupportedException>();
4042
}
4143

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

0 commit comments

Comments
 (0)