Visitar URL original
Error message generation does not work very well for throw cases · Issue #871 · fluentassertions/fluentassertions · GitHub
Skip to content

Error message generation does not work very well for throw cases #871

Closed

Description

@chrischu

Description

When testing that some code should throw an exception the generated error message is not very helpful (especially when using .And).

Complete minimal example reproducing the issue

[Test]
public void Test ()
{
  // ACT
  Action act = () => SomeAction();

  // ASSERT
  act.Should().Throw<SomeException>().And.Property.Should().Be("Value");
}

Actual behavior:

The resulting error message is:

Expected act to be "Value" with length of 5, but "<actualValue>" has a length of <actualLength>.

The current error message is not helpful in several ways:

  • The error message completely ignores the .And and does not mention the actual property (Property)
  • "act" does not represent the called method very well, but this might be very hard to fix

The second part might be very hard (or even impossible to fix), but the first part is very annoying and probably not only affects exception assertions but all kinds of assertions that use .And, although I suspect that it mostly occurs in conjunction with exception assertions (at least in our code that is the only time we use .And).

Expected behavior:

I would expect a message something like this:

Expected Exception.Property to be "Value" with length of 5, but "<actualValue>" has a length of <actualLength>.

Versions

We are using Fluent Assertions v5.3.0 with .NET Framework 4.7.1.

Additional Information

There is a way to work-around this issue (var exception = act.Should().Throw<Exception>().And;) but it feels pretty weird to assign an .And to a variable. In general I think the .And syntax does not work very well with exception assertions.

Activity

  1. jnyrup commented on Jul 25, 2018

    @jnyrup
    Member

    A complete minimal example would be

    // Act
    Action act = () => throw new Exception();
    
    // Assert
    act.Should().Throw<Exception>().Which.Message.Should().Be("Value");

    Message: Expected act to be
    "Value" with a length of 5, but
    "Exception of type 'System.Exception' was thrown." has a length of 48.

    Running the same example in Fluent Assertions 4.19.4 will give you

    'Expected string to be
    "Value" with a length of 5, but
    "Exception of type 'System.Exception' was thrown." has a length of 48.'

    So even though the approach in v5 isn't perfect it is an improvement over v4.

    We're doing textual extraction to try to locate the caller, see CallerIdentifier.DetermineCallerIdentity()

    E.g. this perfectly valid C# code

    act			/*comment*/
    .			/*comment*/
    Should		/*comment*/
    (			/*comment*/
    )			/*comment*/
    .			/*comment*/
    Throw		/*comment*/
    <			/*comment*/
    Exception	/*comment*/
    >			/*comment*/
    (			/*comment*/
    )			/*comment*/
    .			/*comment*/
    Which		/*comment*/
    .			/*comment*/
    Message		/*comment*/
    .			/*comment*/
    Should		/*comment*/
    (			/*comment*/
    )			/*comment*/
    .			/*comment*/
    Be			/*comment*/
    (			/*comment*/
    "Value"		/*comment*/
    )			/*comment*/
    ;

    gives you:

    Message: Expected string to be
    "Value" with a length of 5, but
    "Exception of type 'System.Exception' was thrown." has a length of 48.

    One can always write complex formatted, nested or chained assertions, that we will fail to extract correctly with the textual extraction approach,
    Where by correctly, I mean the sub-assertion that causes the combined assertion to fail.

  2. thomaslevesque commented on Aug 11, 2019

    @thomaslevesque
    Contributor

    I just noticed this issue with a similar example from the documentation:

    object someObject = new Exception("Message");
    someObject.Should().BeOfType<Exception>()
      .Which.Message.Should().Be("Other Message");
    

    This produces the following message:

        Expected someObject to be 
        "Other Message" with a length of 13, but 
        "Message" has a length of 7, differs near "Mes" (index 0).
    

    This is misleading, since it doesn't properly identify the subject.

    One can always write complex formatted, nested or chained assertions, that we will fail to extract correctly with the textual extraction approach,

    I realize that it's not possible to cover all cases, but it would be nice if it worked at least for chained assertions.

  3. self-assigned this
    on Jan 24, 2026
  4. added this to the 8.9 milestone on Feb 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions