Last Updated: February 25, 2016
·
458
· AndrewRadev

Simple ad-hoc rspec matchers

When faced with an include "complicated string" that has to be repeated in the spec, you could do something like this instead:

describe "Frobble page" do
  let(:include_dribble) { include 'super-complicated-string-that-describes-dribble-in-the-page' }

  it "includes dribble only if wibble is set" do
    set_wibble
    page.html.should include_dribble

    unset_wibble
    page.html.should_not include_dribble
  end
end

While you could make a more complicated, global matcher, this kind of ad-hoc thing is easy to spot, local to the describe/context block and provides some stability if the string changes.