本文介绍了Rspec中的自定义失败消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的RSpec + Capybara测试中,当我期望
某项但测试失败时,我想要一些自定义消息。
In my RSpec + Capybara tests, when I'm expecting
something but the test fails, I'd like to have some custom messages.
我通过以下方式实现了这一目标:
I achieved it with:
it "a test" do
do_something
expect(current_path).to eq('/some/path'), "expected path to be 'some_path' but fails"
end
,但是我只想收到我的客户消息,而没有RSpec中的 Failure / Error Failure / Error 行
but what I'd like to have is ONLY my custome message, without the Failure/Error line from RSpec
这可能吗?
推荐答案
如果要自定义输出,则应编写一个自定义格式化程序。基本示例:
If you want to customize the ouput, you should write a custom formatter. Base example:
class MyFormatter
RSpec::Core::Formatters.register self, :example_failed
def initialize(output)
@output = output
end
def example_failed(notification)
@output << "EPIC FAIL! => #{notification.exception}"
end
end
不要忘记要求格式化程序文件并使用-format MyFormatter
来运行套件。
Don't forget to require your formatter file and run your suite with --format MyFormatter
.
您可以在这里找到更复杂的示例:
- https://github.com/mattsears/nyan-cat-formatter
- https://github.com/cupakromer/emoji-rspec
这篇关于Rspec中的自定义失败消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!