Module: Spec::Example::PredicateMatchers

Included in:
ExampleGroupMethods
Defined in:
lib/spec/example/predicate_matchers.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) define_methods_from_predicate_matchers

:nodoc:



36
37
38
39
40
41
42
# File 'lib/spec/example/predicate_matchers.rb', line 36

def define_methods_from_predicate_matchers # :nodoc:
  predicate_matchers.each_pair do |matcher_method, method_on_object|
    define_method matcher_method do |*args|
      eval("be_#{method_on_object.to_s.gsub('?','')}(*args)")
    end
  end
end

- (Object) predicate_matchers

:call-seq:

predicate_matchers[matcher_name] = method_on_object
predicate_matchers[matcher_name] = [method1_on_object, method2_on_object]

Dynamically generates a custom matcher that will match a predicate on your class. RSpec provides a couple of these out of the box:

exist (for state expectations)
  File.should exist("path/to/file")

an_instance_of (for mock argument matchers)
  mock.should_receive(:message).with(an_instance_of(String))

Examples

class Fish
  def can_swim?
    true
  end
end

describe Fish do
  predicate_matchers[:swim] = :can_swim?
  it "should swim" do
    Fish.new.should swim
  end
end


32
33
34
# File 'lib/spec/example/predicate_matchers.rb', line 32

def predicate_matchers
  @predicate_matchers ||= Spec::HashWithDeprecationNotice.new("predicate_matchers", "the new Matcher DSL")
end