123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Rails::Matchers::BaseMatcher Private

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Subclasses:
RSpec::Rails::Matchers::BeANew, RSpec::Rails::Matchers::BeANewRecord, RSpec::Rails::Matchers::HaveEnqueuedMail, RSpec::Rails::Matchers::SendEmail, RSpec::Rails::Matchers::ActionMailbox::Base, RSpec::Rails::Matchers::ActionMailbox::ReceiveInboundEmail, RSpec::Rails::Matchers::ActiveJob::Base, RSpec::Rails::Matchers::ActiveJob::HaveBeenEnqueued, RSpec::Rails::Matchers::ActiveJob::HaveBeenPerformed, RSpec::Rails::Matchers::ActiveJob::HaveEnqueuedJob, RSpec::Rails::Matchers::ActiveJob::HavePerformedJob, RSpec::Rails::Matchers::HaveHttpStatus::GenericStatus, RSpec::Rails::Matchers::HaveHttpStatus::NumericCode, RSpec::Rails::Matchers::HaveHttpStatus::SymbolicStatus, RSpec::Rails::Matchers::RedirectTo::RedirectTo, RSpec::Rails::Matchers::RenderTemplate::RenderTemplateMatcher, RSpec::Rails::Matchers::RoutingMatchers::BeRoutableMatcher, RSpec::Rails::Matchers::RoutingMatchers::RouteToMatcher
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: rspec-rails/lib/rspec/rails/matchers/base_matcher.rb

Overview

Base class to build matchers. Should not be instantiated directly.

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

DefaultFailureMessages - Included

#failure_message

Provides a good generic failure message.

#failure_message_when_negated

Provides a good generic negative failure message.

HashFormatting - Included

#improve_hash_formatting

‘{ :a => 5, :b => 2 }.inspect` produces:

::RSpec::Matchers::Composable - Included

#&
#===

Delegates to #matches?.

#and

Creates a compound and expectation.

#description_of

Returns the description of the given object in a way that is aware of composed matchers.

#or

Creates a compound or expectation.

#values_match?

This provides a generic way to fuzzy-match an expected value against an actual value.

#|
#should_enumerate?

We should enumerate arrays as long as they are not recursive.

#surface_descriptions_in

Transforms the given data structure (typically a hash or array) into a new data structure that, when #inspect is called on it, will provide descriptions of any contained matchers rather than the normal #inspect output.

#unreadable_io?,
#with_matchers_cloned

Historically, a single matcher instance was only checked against a single value.

Class Method Details

.matcher_name

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 90

def self.matcher_name
  @matcher_name ||= underscore(name.split('::').last)
end

.underscore(camel_cased_word) (private)

Borrowed from ActiveSupport.

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 105

def self.underscore(camel_cased_word)
  word = camel_cased_word.to_s.dup
  word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!('-', '_')
  word.downcase!
  word
end

Instance Attribute Details

#actual (readonly)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 16

attr_reader :actual, :expected, :rescued_exception

#diffable?Boolean (readonly)

::RSpec::Rails::Matchers are not diffable by default. Override this to make your subclass diffable.

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 62

def diffable?
  false
end

#expected (readonly)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 16

attr_reader :actual, :expected, :rescued_exception

#expects_call_stack_jump?Boolean (readonly)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 75

def expects_call_stack_jump?
  false
end

#matcher_name (rw)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 95

def matcher_name
  if defined?(@matcher_name)
    @matcher_name
  else
    self.class.matcher_name
  end
end

#matcher_name=(value) (rw)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 19

attr_writer :matcher_name

#rescued_exception (readonly)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 16

attr_reader :actual, :expected, :rescued_exception

#supports_block_expectations?Boolean (readonly)

Most matchers are value matchers (i.e. meant to work with ‘expect(value)`) rather than block matchers (i.e. meant to work with `expect { }`), so this defaults to false. Block matchers must override this to return true.

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 70

def supports_block_expectations?
  false
end

Instance Method Details

#actual_formatted

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 85

def actual_formatted
  RSpec::Support::ObjectFormatter.format(@actual)
end

#assert_ivars(*expected_ivars) (private)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 117

def assert_ivars(*expected_ivars)
  return unless (expected_ivars - present_ivars).any?

  ivar_list = RSpec::Matchers::EnglishPhrasing.list(expected_ivars)
  raise "#{self.class.name} needs to supply#{ivar_list}"
end

#descriptionString

Generates a description using ::RSpec::Matchers::EnglishPhrasing.

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 53

def description
  desc = RSpec::Matchers::EnglishPhrasing.split_words(self.class.matcher_name)
  desc << RSpec::Matchers::EnglishPhrasing.list(@expected) if defined?(@expected)
  desc
end

#expected_formatted

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 80

def expected_formatted
  RSpec::Support::ObjectFormatter.format(@expected)
end

#match_unless_raises(*exceptions)

Used to wrap a block of code that will indicate failure by raising one of the named exceptions.

This is used by rspec-rails for some of its matchers that wrap rails’ assertions.

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 40

def match_unless_raises(*exceptions)
  exceptions.unshift Exception if exceptions.empty?
  begin
    yield
    true
  rescue *exceptions => @rescued_exception
    false
  end
end

#matches?(actual) ⇒ Boolean

Indicates if the match is successful. Delegates to match, which should be defined on a subclass. Takes care of consistently initializing the #actual attribute.

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 29

def matches?(actual)
  @actual = actual
  match(expected, actual)
end

#present_ivars (private)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 124

alias present_ivars instance_variables