1

I have a test that checks several objects in a table on our website. The test is written in SpecFlow and C#

It looks something like this:

When I click proceed
Then I should see the following values
     | key       | value     |
     | tax       | 5.00      |
     | delivery  | 5.00      |
     | subtotal  | 20.00     |

My code behind for the "Then" step is something similar to:

[StepDefinition("I should see the following values")]
public void IShouldSeeTheFollowingValues(Table table)
{
    var basketSummary = new BasketModel();

    foreach (var row in table.Rows)
    {
        switch (row["key"])
        {
            case "tax":
                basketSummary.Tax.Should().Be(row["value"]);
                break;
            case "delivery":
                basketSummary.Delivery.Should().Be(row["value"]);
                break;
            case "subtotal":
                basketSummary.Subtotal.Should().Be(row["value"]);
                break;
        }
    }
}

The problem with this is in our build logs if the test errors it looks something like this:

When I click proceed
-> done: OrderConfirmationPageSteps.ClickProceed() (1.0s)
Then I should see the following values
  --- table step argument ---
     | key       | value     |
     | tax       | 5.00      |
     | delivery  | 5.00      |
     | subtotal  | 20.00     |
-> error: Expected value to be 5.00, but found 1.00.

as you can see above its hard to distinguish which object it means... when it says it expects it to be 5.00 Is there a way I can modify the output to say something along the lines of:

-> error: Expected value of Tax to be 5.00, but found 1.00.
Festivejelly
  • 670
  • 2
  • 12
  • 30

2 Answers2

1

You can do two things:

  1. Pass a reason phrase to the Be() method, e.g. `basketSummary.Delivery.Should().Be(row["value"], "because that's the tax value");
  2. Wrap the call in an AssertionScope and pass the description (the context) into its constructor, like this
Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
0

In the latest version https://fluentassertions.com/introduction#subject-identification

string username = "dennis";
username.Should().Be("jonas");
//will throw a test framework-specific exception with the following message:

Expected username to be "jonas" with a length of 5,
 but "dennis" has a length of 6, differs near "den" (index 0).

Fluent Assertions can use the C# code of the unit test to extract the name of the subject and use that in the assertion failure.

Since it needs the debug symbols for that, this will require you to compile the unit tests in debug mode, even on your build servers.

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170