Class: RSpec::Matchers::BuiltIn::BePredicate

Inherits:
Be show all
Defined in:
lib/rspec/matchers/built_in/be.rb

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual, #expected, #rescued_exception

Instance Method Summary (collapse)

Methods inherited from Be

#match

Methods inherited from BaseMatcher

#==, #diffable?, #match_unless_raises

Methods included from Pretty

#_pretty_print, #expected_to_sentence, #name, #name_to_sentence, #split_words, #to_sentence, #to_word, #underscore

Constructor Details

- (BePredicate) initialize(*args, &block)

A new instance of BePredicate



130
131
132
133
134
# File 'lib/rspec/matchers/built_in/be.rb', line 130

def initialize(*args, &block)
  @expected = parse_expected(args.shift)
  @args = args
  @block = block
end

Instance Method Details

- (Object) description



164
165
166
# File 'lib/rspec/matchers/built_in/be.rb', line 164

def description
  "#{prefix_to_sentence}#{expected_to_sentence}#{args_to_sentence}"
end

- (Object) failure_message_for_should



156
157
158
# File 'lib/rspec/matchers/built_in/be.rb', line 156

def failure_message_for_should
  "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}"
end

- (Object) failure_message_for_should_not



160
161
162
# File 'lib/rspec/matchers/built_in/be.rb', line 160

def failure_message_for_should_not
  "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
end

- (Boolean) matches?(actual)

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rspec/matchers/built_in/be.rb', line 136

def matches?(actual)
  @actual = actual

  if is_private_on?( @actual )
    raise Expectations::ExpectationNotMetError.new("expectation set on private method `#{predicate}`")
  end

  begin
    return @result = actual.__send__(predicate, *@args, &@block)
  rescue NameError => predicate_missing_error
    "this needs to be here or rcov will not count this branch even though it's executed in a code example"
  end

  begin
    return @result = actual.__send__(present_tense_predicate, *@args, &@block)
  rescue NameError
    raise predicate_missing_error
  end
end