Custom failure messages in Rspec

谁说我不能喝 提交于 2019-12-12 12:22:34

问题


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

but what I'd like to have is ONLY my custome message, without the Failure/Error line from RSpec

Is this possible?


回答1:


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

Don't forget to require your formatter file and run your suite with --format MyFormatter.

You could find a more complex example here: http://eftimov.net/how-to-write-rspec-formatters-from-scratch

Or find inspiration with other popular formatters:

  • https://github.com/mattsears/nyan-cat-formatter
  • https://github.com/cupakromer/emoji-rspec


来源:https://stackoverflow.com/questions/34288211/custom-failure-messages-in-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!