Module: CapybaraTestHelpers::Assertions

Included in:
TestHelper
Defined in:
lib/capybara_test_helpers/assertions.rb

Overview

Internal: Wraps RSpec matchers to allow using them after calling ‘should` or `should_not` to perform the assertion.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs, &block) ⇒ Object (private)

Internal: Override the method_missing defined in RSpec::Matchers to avoid accidentally calling a predicate or has matcher instead of an assertion.



117
118
119
120
121
122
123
124
# File 'lib/capybara_test_helpers/assertions.rb', line 117

def method_missing(method, *args, **kwargs, &block)
  case method.to_s
  when CapybaraTestHelpers::TestHelper::DYNAMIC_MATCHER_REGEX
    raise NoMethodError, "undefined method `#{ method }' for #{ inspect }.\nUse `delegate_to_test_context(:#{ method })` in the test helper class if you plan to use it often, or `test_context.#{ method }` as needed in the instance."
  else
    super
  end
end

Instance Method Details

#have(*args, **kwargs, &filter) ⇒ Object

Public: Allows to call have_selector with a shorter syntax.



97
98
99
100
101
102
103
# File 'lib/capybara_test_helpers/assertions.rb', line 97

def have(*args, **kwargs, &filter)
  if args.first.is_a?(Integer)
    ::RSpec::CollectionMatchers::Have.new(*args, **kwargs)
  else
    have_selector(*args, **kwargs, &filter)
  end
end

#have_value(expected_value, **options) ⇒ Object

Public: Allows to check on any input value asynchronously.



108
109
110
111
# File 'lib/capybara_test_helpers/assertions.rb', line 108

def have_value(expected_value, **options)
  synchronize_expectation(**options) { expect(value).to_or not_to, eq(expected_value) }
  self
end

#invert_expectationObject

Public: Allows to write complex nested assertions.



34
35
36
# File 'lib/capybara_test_helpers/assertions.rb', line 34

def invert_expectation
  should(!not_to)
end

#not_toObject Also known as: or_should_not

Public: Makes it more readable when in used in combination with to_or.

Raises:

  • (ArgumentError)


26
27
28
29
30
# File 'lib/capybara_test_helpers/assertions.rb', line 26

def not_to
  raise(ArgumentError, 'You must call `should` or `should_not` before calling this method') if @negated.nil?

  @negated
end

#should(negated = false) ⇒ Object

Public: Returns a test helper with a positive assertion state. Any assertions called after it will execute as ‘expect(…).to …`.



10
11
12
13
14
15
# File 'lib/capybara_test_helpers/assertions.rb', line 10

def should(negated = false)
  negated = !!negated # Coerce to boolean.
  return self if negated == @negated

  clone.tap { |test_helper| test_helper.instance_variable_set('@negated', negated) }
end

#should_notObject

Public: Returns a test helper with a negative assertion state. Any assertions called after it will execute as ‘expect(…).not_to …`.



20
21
22
# File 'lib/capybara_test_helpers/assertions.rb', line 20

def should_not
  @negated ? self : should(true)
end