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
With the current async support in FA, it's too easy in my opinion to accidentally use .Should().NotThrow() against an async void lambda. This will result in a test that looks valid but doesn't actually test the continuation.
Example:
ActionasyncFunc=async()=>{await Task.Delay(5000);thrownew Exception("fail");};
asyncFunc.Should().NotThrow();// Test (incorrectly) passes
...instead of the correct test implementation:
Func<Task>asyncFunc=async()=>{await Task.Delay(5000);thrownew Exception("fail");};
asyncFunc.Should().NotThrow();// Test (correctly) fails
I see a couple of separate but complementary possible improvements:
Detect that the Action is an async method (e.g. action.Method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null), and throw. Would break anyone intentionally sending in an async void, but that seems unlikely. I don't believe it's possible to "fix" the async void method; if it was that would be another alternative.
Add actual async [Not]ThrowAsync methods that only work on Func<Task>. Wouldn't directly prevent this mistake if NotThrow was still used, but the added Async (and await) would make it more obvious when the async assertions aren't being used. I don't know if there was a reason AsyncFunctionAssertions was designed to block instead of using async/await (possibly because it would be a bit less fluent), but personally I'd much prefer actual async assertions.
The text was updated successfully, but these errors were encountered:
zarenner
changed the title
Too easy to accidentally use Should().NotThrow() against async void lamda
Too easy to accidentally use Should().NotThrow() against async void lambda
Dec 21, 2017
With the current async support in FA, it's too easy in my opinion to accidentally use .Should().NotThrow() against an async void lambda. This will result in a test that looks valid but doesn't actually test the continuation.
Example:
...instead of the correct test implementation:
I see a couple of separate but complementary possible improvements:
Detect that the Action is an async method (e.g. action.Method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null), and throw. Would break anyone intentionally sending in an async void, but that seems unlikely. I don't believe it's possible to "fix" the async void method; if it was that would be another alternative.
Add actual async [Not]ThrowAsync methods that only work on Func<Task>. Wouldn't directly prevent this mistake if NotThrow was still used, but the added Async (and await) would make it more obvious when the async assertions aren't being used. I don't know if there was a reason AsyncFunctionAssertions was designed to block instead of using async/await (possibly because it would be a bit less fluent), but personally I'd much prefer actual async assertions.
The text was updated successfully, but these errors were encountered: