question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Should().Throw() with Func<T> fails after upgrading to version 5.5.0

See original GitHub issue

Description

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:closed
  • Created 5 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
jnyrupcommented, Nov 15, 2018

It seems doable with the following new overload of Should()

public static AsyncFunctionAssertions Should<T>(this Func<Task<T>> action)
{
    return new AsyncFunctionAssertions(action, extractor);
}
1reaction
krajekcommented, Nov 15, 2018

I will take a look in the evening.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found