Module: RSpec::Matcher::ClassMethods

Defined in:
lib/rspec/matcher.rb

Overview

Methods added as class method to includer

Instance Method Summary collapse

Instance Method Details

#register_as(name, options = {}) ⇒ void

Note:

self.empty = true is called on initialize

This method returns an undefined value.

Registers this matcher in RSpec. Optionally sets options via setter methods on initialize.

Examples:

class BeNil
  include RSpec::Matcher
  register_as "be_nil"
  register_as "be_empty", empty: true
  #...
end

expect(nil).to be_nil
expect([]).to be_empty

Parameters:

  • name (String)

    what to register this matcher as



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rspec/matcher.rb', line 82

def register_as name, options = {}
  s = self
  m = Module.new do
    define_method name do |expected = UNDEFINED, *args, &block|
      s.new(expected, options, *args, &block)
    end
  end

  RSpec.configure do |config|
    config.include m
  end
end