fix: throw InvalidOperationException when using async void delegates#652
Merged
fix: throw InvalidOperationException when using async void delegates#652
InvalidOperationException when using async void delegates#652Conversation
The using an `async void` delegate, it is not possible to await the outcome. Therefore, it now throws an `InvalidOperationException` in this case.
There was a problem hiding this comment.
Pull Request Overview
This pull request ensures that using aweXpect on async void delegates now throws an InvalidOperationException to prevent silent failures when awaiting outcomes.
- Added tests for async void delegate scenarios with and without CancellationToken
- Modified Expect.cs to use the updated DelegateSource constructors
- Updated DelegateSource to include a check for async void methods via ThrowIfAsyncVoid
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| Tests/aweXpect.Internal.Tests/ThatTests/DelegateTests.cs | Added tests verifying that async void delegates throw with appropriate messages |
| Source/aweXpect.Core/Expect.cs | Updated delegate handling in the factory method to delegate null handling to DelegateSource |
| Source/aweXpect.Core/Core/Sources/DelegateSource.cs | Introduced overloads and ThrowIfAsyncVoid method to guard against async void delegates |
Comments suppressed due to low confidence (1)
Source/aweXpect.Core/Core/Sources/DelegateSource.cs:16
- Consider adding targeted unit tests for the ThrowIfAsyncVoid method to verify that it does not throw for valid synchronous methods and reliably throws for async void implementations.
ThrowIfAsyncVoid(action?.GetMethodInfo(), "Func<CancellationToken, Task>");
|
Contributor
Test Results 4 files ±0 4 suites ±0 22s ⏱️ ±0s Results for commit 22cbb8a. ± Comparison against base commit 7d6a7be. This pull request removes 54 and adds 54 tests. Note that renamed tests count towards both. |
Contributor
|
This is addressed in release v2.18.0. |
This was referenced Sep 1, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



This pull request ensures that using aweXpect on async void delegates now throws an
InvalidOperationExceptionto prevent silent failures when awaiting outcomes.