Module: Capybara::Compose::Assertions

Included in:
TestHelper
Defined in:
lib/capybara/compose/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.



126
127
128
129
130
131
132
133
# File 'lib/capybara/compose/assertions.rb', line 126

def method_missing(method, *args, **kwargs, &block)
  case method.to_s
  when 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.



102
103
104
105
106
107
108
# File 'lib/capybara/compose/assertions.rb', line 102

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.



113
114
115
116
# File 'lib/capybara/compose/assertions.rb', line 113

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.



39
40
41
# File 'lib/capybara/compose/assertions.rb', line 39

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)


31
32
33
34
35
# File 'lib/capybara/compose/assertions.rb', line 31

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 …`.



15
16
17
18
19
20
# File 'lib/capybara/compose/assertions.rb', line 15

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 …`.



25
26
27
# File 'lib/capybara/compose/assertions.rb', line 25

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