1

我有一个测试可以检查我们网站上表格中的几个对象。测试是用 SpecFlow 和 C# 编写的

它看起来像这样:

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

我的“然后”步骤背后的代码类似于:

[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;
        }
    }
}

如果测试错误看起来像这样,那么问题就在我们的构建日志中:

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.

正如您在上面看到的那样,很难区分它意味着哪个对象......当它说它期望它是 5.00 时有没有办法可以修改输出以说出以下内容:

-> error: Expected value of Tax to be 5.00, but found 1.00.
4

2 回答 2

0

你可以做两件事:

  1. 向方法传递一个原因短语Be(),例如 `basketSummary.Delivery.Should().Be(row["value"], "because that's the tax value");
  2. 将调用包装在 an 中AssertionScope并将描述(上下文)传递给它的构造函数,如下 所示
于 2016-02-03T17:52:21.337 回答
0

在最新版本中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 可以使用单元测试的 C# 代码来提取主题的名称,并在断言失败时使用它。

由于它需要调试符号,这将要求您在调试模式下编译单元测试,即使在您的构建服务器上也是如此。

于 2021-01-30T00:01:26.677 回答