Using Should().Throw() with Func<T> fails after upgrading to version 5.5.0
See original GitHub issueDescription
After upgrading FluentAssertions from version 5.4.2 to 5.5.0, usage of Should().Throw<T> with async method does not yield expected behavior. The following code example functions as expected with version 5.4.2.
Complete minimal example reproducing the issue
public class FluentAssertionsExceptionTests
{
[Fact]
public void Test1()
{
Func<Task<int>> func1 = () => TestAsync(false);
func1.Should().NotThrow<Exception>();
// Does not assert thrown exception
Func<Task<int>> func2 = () => TestAsync(true);
func2.Should().Throw<Exception>();
}
private async Task<int> TestAsync(bool throwException)
{
if (throwException)
{
throw new Exception("Error");
}
return await Task.FromResult(123);
}
}
Expected behavior:
The assertion for the thrown exception passes.
Actual behavior:
The assertion for the thrown exception does not pass. The assertion does not detect the thrown exception.
Xunit.Sdk.XunitException
HResult=0x80131500
Message=Expected System.Exception, but no exception was thrown.
Source=FluentAssertions
StackTrace:
at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message)
at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
at FluentAssertions.Specialized.FunctionAssertions`1.Throw[TException](Exception exception, String because, Object[] becauseArgs)
at FluentAssertions.Specialized.FunctionAssertions`1.Throw[TException](String because, Object[] becauseArgs)
Versions
Visual Studio 2017, .NET Core 2.1, XUnit 2.4.1, FluentAssertions 5.5.0
Additional Information
Removal of the ‘TResult’ argument from the Func<Task<T>> yields expected behavior and the assertion functions as expected with version 5.5.0.
Func<Task> func3 = () => TestAsync(true);
func3.Should().Throw<Exception>();
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
php - How can I manually return or throw a validation error ...
I can't write proper validation stuff to use with the validate method here, but, I really like how Laravel works when the validation...
Read more >Error Handling | Documentation - Swift.org
When a function throws an error, it changes the flow of your program, ... func canThrowErrors() throws -> String func cannotThrowErrors() -> String....
Read more >Basics & Advanced PHP Exception Handling Tutorial
Read this PHP exception handling tutorial to understand how to use PHP try catch blocks. Understand advanced uses with multiple exception ...
Read more >Version 5 - 5.21.2 Release Notes
[DT-026139] Installing Portal Authenticator module throws error and SHM fails to restart after; [DT-026026] Checkpoint Comments do not get saved ...
Read more >Exceptions - Manual
The thrown object must be an instanceof Throwable. Trying to throw an object that is not will result in a PHP Fatal Error....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It seems doable with the following new overload of
Should()
I will take a look in the evening.