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

Inherits:
BaseMatcher
  • Object
show all
Includes:
BeHelpers
Defined in:
lib/rspec/matchers/built_in/be.rb

Constant Summary

Constant Summary

Constants inherited from BaseMatcher

RSpec::Matchers::BuiltIn::BaseMatcher::UNDEFINED

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual, #expected, #rescued_exception

Instance Method Summary (collapse)

Methods inherited from BaseMatcher

#diffable?, #match_unless_raises, #supports_block_expectations?

Methods included from MatchAliases

#==

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)

A new instance of BePredicate

136
137
138
139
140
# File 'lib/rspec/matchers/built_in/be.rb', line 136
def initialize(*args, &block)
  @expected = parse_expected(args.shift)
  @args = args
  @block = block
end

Instance Method Details

- (Object) description

178
179
180
# File 'lib/rspec/matchers/built_in/be.rb', line 178
def description
  "#{prefix_to_sentence}#{expected_to_sentence}#{args_to_sentence}"
end

- (Object) failure_message_for_should

170
171
172
# File 'lib/rspec/matchers/built_in/be.rb', line 170
def failure_message_for_should
  "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}"
end

- (Object) failure_message_for_should_not

174
175
176
# File 'lib/rspec/matchers/built_in/be.rb', line 174
def failure_message_for_should_not
  "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
end

- (Boolean) is_private_on?(actual)

Returns:

  • (Boolean)
186
187
188
# File 'lib/rspec/matchers/built_in/be.rb', line 186
def is_private_on? actual
  actual.private_methods.include? predicate.to_s
end

- (Boolean) matches?(actual) Also known as: ===

Returns:

  • (Boolean)
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rspec/matchers/built_in/be.rb', line 142
def matches?(actual)
  @actual = actual
  if is_private_on?( @actual )
    RSpec.deprecate "matching with be_#{predicate.to_s.gsub(/\?$/,'')} on private method #{predicate}",
      :replacement => "`expect(object.send(#{predicate.inspect})).to be_true` or change the method's visibility to public",
      :call_site => caller(0)[3]
  end
  begin
    @result = actual.__send__(predicate, *@args, &@block)
    check_respond_to(predicate)
    return @result
  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
    @result = actual.__send__(present_tense_predicate, *@args, &@block)
    check_respond_to(present_tense_predicate)
    return @result
  rescue NameError
    raise predicate_missing_error
  end
end