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

Inherits:
Object
  • Object
show all
Includes:
Matchers::Composable, DefaultFailureMessages
Defined in:
lib/rspec/rails/matchers/base_matcher.rb

Overview

@ api private

Defined Under Namespace

Modules: DefaultFailureMessages

Constant Summary collapse

UNDEFINED =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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

Instance Method Summary collapse

Methods included from DefaultFailureMessages

#failure_message, #failure_message_when_negated

Constructor Details

#initialize(expected = UNDEFINED) ⇒ BaseMatcher

Returns a new instance of BaseMatcher


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

def initialize(expected = UNDEFINED)
  @expected = expected unless UNDEFINED.equal?(expected)
end

Instance Method Details

#descriptionString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates a description using Matchers::EnglishPhrasing.

Returns:

  • (String)

51
52
53
54
55
# File 'lib/rspec/rails/matchers/base_matcher.rb', line 51

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

#diffable?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Boolean)

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

def diffable?
  false
end

#expects_call_stack_jump?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

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

def expects_call_stack_jump?
  false
end

#match_unless_raises(*exceptions) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.


38
39
40
41
42
43
44
45
46
# File 'lib/rspec/rails/matchers/base_matcher.rb', line 38

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

#matches?(actual) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Boolean)

27
28
29
30
# File 'lib/rspec/rails/matchers/base_matcher.rb', line 27

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

#supports_block_expectations?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Returns:

  • (Boolean)

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

def supports_block_expectations?
  false
end