EricC
EricC

Reputation: 1439

RSpec custom matcher with arguments

I am following Michael Hartl's rails tutorial Ch.8 exercise 2 that asks to decouple test from implementation by defining RSpec custom matchers.

One of the definition I have is

RSpec::Matchers.define :have_error_message do |message|
  match do |page|
    page.should have_selector('div.alert.alert-error', text: message)
  end
end

So I can write the following test in RSpec

it { should have_error_message('Invalid') }

However, calling have_error_message without an argument like below also works.

it { should_not have_error_message } 

How come this doesn't give an error (the argument is missing)? What value does the message variable in the custom matcher take?

Upvotes: 1

Views: 721

Answers (1)

Ultimation
Ultimation

Reputation: 1069

The argument defaults as nil, so it would expect an error with a nil text value like:

page.should have_selector('div.alert.alert-error', text: nil)

Upvotes: 1

Related Questions