fabdurso
fabdurso

Reputation: 2444

Custom failure messages in Rspec

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

console

Is this possible?

Upvotes: 4

Views: 1648

Answers (1)

markets
markets

Reputation: 7043

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:

Upvotes: 2

Related Questions