Method: RSpec::Matchers::DSL::Macros#chain

Defined in:
lib/rspec/matchers/dsl.rb

#chain(method_name, *attr_names, &definition) ⇒ Object

Convenience for defining methods on this matcher to create a fluent interface. The trick about fluent interfaces is that each method must return self in order to chain methods together. chain handles that for you. If the method is invoked and the include_chain_clauses_in_custom_matcher_descriptions config option hash been enabled, the chained method name and args will be added to the default description and failure message.

In the common case where you just want the chained method to store some value(s) for later use (e.g. in match), you can provide one or more attribute names instead of a block; the chained method will store its arguments in instance variables with those names, and the values will be exposed via getters.

Examples:


RSpec::Matchers.define :have_errors_on do |key|
  chain :with do |message|
    @message = message
  end

  match do |actual|
    actual.errors[key] == @message
  end
end

expect(minor).to have_errors_on(:age).with("Not old enough to participate")


298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/rspec/matchers/dsl.rb', line 298

def chain(method_name, *attr_names, &definition)
  unless block_given? ^ attr_names.any?
    raise ArgumentError, "You must pass either a block or some attribute names (but not both) to `chain`."
  end

  definition = assign_attributes(attr_names) if attr_names.any?

  define_user_override(method_name, definition) do |*args, &block|
    super(*args, &block)
    @chained_method_clauses.push([method_name, args])
    self
  end
end