如何通过流畅的断言报告对象的名称
本文关键字:对象 报告 断言 何通过 | 更新日期: 2023-09-27 18:32:58
>我有一个测试,可以检查我们网站上表格中的几个对象。该测试是用 SpecFlow 和 C# 编写
的它看起来像这样:
When I click proceed
Then I should see the following values
| key | value |
| tax | 5.00 |
| delivery | 5.00 |
| subtotal | 20.00 |
我的"then"步骤的代码隐藏类似于:
[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.
您可以做两件事:
- 将原因短语传递给
Be()
方法,例如'basketSummary.Delivery.Should().Be(row["value"], "因为那是税值"); - 将调用包装在
AssertionScope
中,并将描述(上下文)传递到其构造函数中,例如这
在最新版本中 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).
流畅断言可以使用单元测试的 C# 代码来提取主题的名称,并在断言失败中使用该名称。
由于它需要调试符号,这将需要您在调试模式下编译单元测试,即使在生成服务器上也是如此。