Contents

This page comes from RSpec Expectations 2.3.0. It is not part of the 3.12.2 docs. See the latest RSpec Expectations docs.

Define matcher with fluent interface

Use the chain() method to define matchers with a fluent interface.

Scenario: chained method with argumetn

  • Given a file named "between_spec.rb" with:
    ruby
    RSpec::Matchers.define :be_bigger_than do |first|
      match do |actual|
        (actual > first) && (actual < @second)
      end
    
      chain :but_smaller_than do |second|
        @second = second
      end
    end
    
    describe 5 do
      it { should be_bigger_than(4).but_smaller_than(6) }
    end
  • When I run "rspec between_spec.rb --format documentation"
  • Then the output should contain "1 example, 0 failures"
  • And the output should contain "should be bigger than 4"

Source: features/matchers/define_matcher_with_fluent_interface.feature at v2.3.0, RSpec Expectations (MIT license).