Skip to content

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

@chrischu

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions