Class: RSpec::Rails::Matchers::BaseMatcher Private
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
-
UNDEFINED =
Used to detect when no arg is passed to
initialize
.nil
cannot be used because it’s a valid value to pass.Object.new.freeze
Class Method Summary
- .matcher_name Internal use only
- .new(expected = UNDEFINED) ⇒ BaseMatcher constructor Internal use only
-
.underscore(camel_cased_word)
private
Internal use only
Borrowed from ActiveSupport.
Instance Attribute Summary
- #actual readonly Internal use only
-
#diffable? ⇒ Boolean
readonly
Internal use only
::RSpec::Rails::Matchers
are not diffable by default. - #expected readonly Internal use only
- #expects_call_stack_jump? ⇒ Boolean readonly Internal use only
- #matcher_name rw Internal use only
- #matcher_name=(value) rw Internal use only
- #rescued_exception readonly Internal use only
-
#supports_block_expectations? ⇒ Boolean
readonly
Internal use only
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.
Instance Method Summary
- #actual_formatted Internal use only
-
#description ⇒ String
Internal use only
Generates a description using
::RSpec::Matchers::EnglishPhrasing
. - #expected_formatted Internal use only
-
#match_unless_raises(*exceptions)
Internal use only
Used to wrap a block of code that will indicate failure by raising one of the named exceptions.
-
#matches?(actual) ⇒ Boolean
Internal use only
Indicates if the match is successful.
- #assert_ivars(*expected_ivars) private Internal use only
- #present_ivars private Internal use only
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 | ‘{ |
::RSpec::Matchers::Composable
- Included
#& | Alias for Matchers::Composable#and. |
#=== | Delegates to #matches?. |
#and | Creates a compound |
#description_of | Returns the description of the given object in a way that is aware of composed matchers. |
#or | Creates a compound |
#values_match? | This provides a generic way to fuzzy-match an expected value against an actual value. |
#| | Alias for Matchers::Composable#or. |
#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 |
#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.
# 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.
# 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)
# 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 ]
#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.
# 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
#description ⇒ String
Generates a description using ::RSpec::Matchers::EnglishPhrasing
.
# 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.
# 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.
#present_ivars (private)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/base_matcher.rb', line 124
alias present_ivars instance_variables