如果FluentAssertions中的AssertionScope失败,如何在第一行插入消息



我循环遍历一个结果数组,并将每个对象与预期结果数组resultItem.Should().BeEquivalentTo(expectedResultItem)进行比较。每个项目都有一个唯一的标识符,并希望在上面显示它。

这是一个示例结果,前两行来自BeEquivalentTo的单个调用,而第三个条目来自另一个:

Expected property resultItem.PropertyA to be "85.01", but "85.00" differs near "0" (index 4).
Expected property resultItem.PropertyB to be 85.01M, but found 85.00M.
Expected step to be MyProject.Models.ResultItem
{
IdA = "620",
IdB = "Total", 
PropertyA = "12156.00", 
PropertyB = 12156.0M, 
PropertyC = 33
}, but found <null>.

是否可以在每个结果上方插入更多细节作为某种形式的标题?类似这样的东西:

Error found on entry (IdA:12, IdB: "ABC")
Expected property resultItem.PropertyA to be "85.01", but "85.00" differs near "0" (index 4).
Expected property resultItem.PropertyB to be 85.01M, but found 85.00M.
Error found on entry (IdA:620, IdB: "Total")
Expected step to be MyProject.Models.ResultItem
{
IdA = "620",
IdB = "Total", 
PropertyA = "12156.00", 
PropertyB = 12156.0M, 
PropertyC = 33
}, but found <null>.

虽然这不是理想的可读性,但您可以滥用FA的because参数,让它输出一些上下文:

[TestMethod]
public void AssertionContext()
{
using (new AssertionScope())
{
Foo resultItem = new Foo { PropA = "123.0", PropB = 123.0m, Id = 777 };
resultItem.Should().BeEquivalentTo(new { PropA = "123.1", PropB = 123.1m }, "ID {0} should match", resultItem.Id);
Foo step = null;
step.Should().BeEquivalentTo(new Foo { PropA = "123.0", PropB = 123.0m }, "ID {0} should match", 987654);
}
}
Expected property resultItem.PropA to be "123.1" because ID 777 should match, but "123.0" differs near "0" (index 4).
Expected property resultItem.PropB to be 123.1M because ID 777 should match, but found 123.0M.
Expected step to be Foo
{
Id = 0, 
PropA = "123.0", 
PropB = 123.0M
} because ID 987654 should match, but found <null>.

相关内容

  • 没有找到相关文章

最新更新