如何实现 XUnit 描述性断言消息? [英] How to implement XUnit descriptive Assert message?

查看:32
本文介绍了如何实现 XUnit 描述性断言消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

在 XUnit github 中我发现了这个:添加 Assert.Equal(expected, actual, message) 重载 #350 (因此开发人员要求不存在的重载,见下文)

in XUnit github I found this: Add Assert.Equal(expected, actual, message) overload #350 (so a developer ask for a non existing overload see below)

引用答案:

我们相信自记录代码;包括您的断言.

We are a believer in self-documenting code; that includes your assertions.

(所以 XUnit 团队拒绝了)

好的,我知道了.我也相信自我记录代码.我还是不知道这个用例:

OK, I got it. I also believe the self documenting code. Still I can not find out this use case:

示例

// Arrange
// Create some external soap service client and its wrapper classes

// Act
// client.SomeMethod();

// Assert
// Sorry, soap service's interface, behaviour and design is *given*
// So I have to check if there is no Error, and 
// conveniently if there is, then I would like to see it in the assertion message

Assert.Equal(0, client.ErrorMessage.Length); // Means no error

// I would like to have the same result what would be the following *N*U*n*i*t* assert:
// Assert.AreEqual(0, client.ErrorMessage.Length, client.ErrorMessage); // Means no error

问题

在这种情况下,我如何在 XUnit 中实现描述性断言消息,但仍然没有这种重载?

How can I implement a descriptive assert message in this case in XUnit which still has no such an overload?

推荐答案

使用链接中提供的建议.像 fluent 断言 或创建您自己的包含 Assert.True 或 Assert.False 留下了他们的消息过载.它被进一步提及

Use the suggestions provided at the link. Like fluent assertions or create your own assertion that wraps the Assert.True or Assert.False which were left with their message overloads. It was mentioned further down

引用

您可以向 Assert.True 和 .False 提供消息.如果你只是不能没有消息(并拒绝使用不同的断言),你总是可以回退到:

You can provide messages to Assert.True and .False. If you simply cannot live without messages (and refuse to use a different assertion), you could always fall back to:

Assert.True(number == 2, "This is my message");

引用:

如果你真的想有消息,你可以添加 Fluent断言或者也许xbehave到您的测试项目并使用它们的语法.如果 Fluent Assertions 遇到它的存在,它甚至会抛出 xunit.net 异常.

If you really want to have messages you could add Fluent Assertions or maybe xbehave to your test projects and use their syntax. Fluent Assertions even throws xunit.net exceptions if it encounters its presence.

这篇关于如何实现 XUnit 描述性断言消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆