如何将消息附加到 RSpec 检查? [英] How to attach a message to RSpec check?

查看:39
本文介绍了如何将消息附加到 RSpec 检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 RSpec 中:我是否可以像在 xUnit 风格的测试框架中那样将消息附加到检查中?怎么样?

assert_equal value1, value2, '出了点问题'

解决方案

对于 RSpec 3+:

可以将消息自定义为字符串或使用 proc(检查参考).

expect(1).to eq(2), '一个不是两个!'

<块引用>

自定义消息RSpec 尝试提供有用的失败消息,但对于您需要更多信息的情况具体信息,您可以在示例中定义自己的消息.这适用于运算符匹配器以外的任何匹配器.

来源@relishapp


对于较旧的 RSpec 版本

应该should_not 接受第二个参数(message) 覆盖匹配器的默认消息.

1.should be(2), '一个不是两个!'

不过,默认消息通常非常有用.

In RSpec: Can I attach a message to a check the same way as I would do in xUnit style test frameworks? How?

assert_equal value1, value2, 'something is wrong'

解决方案

For RSpec 3+:

The message could be customized as a string or using a proc(check the reference).

expect(1).to eq(2), 'one is not two!'

Customized message RSpec tries to provide useful failure messages, but for cases in which you want more specific information, you can define your own message right in the example. This works for any matcher other than the operator matchers.

source @ relishapp


For older RSpec versions

should and should_not take a second argument (message) that overrides the matcher’s default message.

1.should be(2), 'one is not two!'

The default messages are usually pretty useful though.

这篇关于如何将消息附加到 RSpec 检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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