Class: Capybara::Minitest::Assertions::MatcherAssertion

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/minitest/assertions/matcher.rb

Overview

Represents an assertion based on a Capybara RSpec matcher.

Direct Known Subclasses

MatcherRefutation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher) ⇒ MatcherAssertion

Initializes a new assertion with the given matcher.



64
65
66
# File 'lib/capybara/minitest/assertions/matcher.rb', line 64

def initialize(matcher)
  @matcher = matcher
end

Instance Attribute Details

#matcherObject (readonly)

The matcher this assertion is based on.



61
62
63
# File 'lib/capybara/minitest/assertions/matcher.rb', line 61

def matcher
  @matcher
end

Class Method Details

.matcher_failure_message_methodObject

The failure message method called on the RSpec matcher for this kind of assertion.



104
105
106
# File 'lib/capybara/minitest/assertions/matcher.rb', line 104

def self.matcher_failure_message_method
  :failure_message
end

.matcher_test_methodObject

The test method called on the RSpec matcher for this kind of assertion.



98
99
100
# File 'lib/capybara/minitest/assertions/matcher.rb', line 98

def self.matcher_test_method
  :matches?
end

.name_prefixObject

The prefix for method names of this kind of assertion.



92
93
94
# File 'lib/capybara/minitest/assertions/matcher.rb', line 92

def self.name_prefix
  'assert'
end

Instance Method Details

#nameObject

The name of the assertion.

assertion.matcher.name # => have_text
assertion.name         # => assert_has_text


73
74
75
# File 'lib/capybara/minitest/assertions/matcher.rb', line 73

def name
  "#{self.class.name_prefix}_#{@matcher.third_person_name}"
end

#test(subject, *args) ⇒ Object

Executes the assertion by testing it against the matcher on the given test subject and with the provided arguments. Returns an array with the test result (as a boolean) and the failure message.



80
81
82
83
84
85
86
87
88
89
# File 'lib/capybara/minitest/assertions/matcher.rb', line 80

def test(subject, *args)
  # Create an RSpec matcher object.
  matcher = @matcher.matcher(*args)
  # Perform the match.
  test_result = matcher.send(self.class.matcher_test_method, subject)
  # Get the failure message, which has to be done after the matching.
  failure_message = matcher.send(self.class.matcher_failure_message_method)

  [test_result, failure_message]
end