Class: RSpec::Matchers::BuiltIn::Exist

Inherits:
BaseMatcher show all
Defined in:
lib/rspec/matchers/built_in/exist.rb

Constant Summary

Constant Summary

Constants inherited from BaseMatcher

BaseMatcher::UNDEFINED

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual, #expected, #rescued_exception

Instance Method Summary (collapse)

Methods inherited from BaseMatcher

#description, #diffable?, #failure_message_for_should, #failure_message_for_should_not, #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

- (Exist) initialize(*expected)

A new instance of Exist

5
6
7
# File 'lib/rspec/matchers/built_in/exist.rb', line 5
def initialize(*expected)
  @expected = expected
end

Instance Method Details

- (Boolean) matches?(actual)

Returns:

  • (Boolean)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rspec/matchers/built_in/exist.rb', line 9
def matches?(actual)
  @actual = actual
  predicates = [:exist?, :exists?].select { |p| @actual.respond_to?(p) }
  existence_values = predicates.map { |p| @actual.__send__(p, *@expected) }
  uniq_truthy_values = existence_values.map { |v| !!v }.uniq
  case uniq_truthy_values.size
  when 0; raise NoMethodError.new("#{@actual.inspect} does not respond to either #exist? or #exists?")
  when 1; existence_values.first
  else raise "#exist? and #exists? returned different values:\n\n" +
    " exist?: #{existence_values.first}\n" +
    "exists?: #{existence_values.last}"
  end
end