module CapybaraTestHelpers::Assertions

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

Public Instance Methods

have(*args, **kwargs, &filter) click to toggle source

Public: Allows to call have_selector with a shorter syntax.

# 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) click to toggle source

Public: Allows to check on any input value asynchronously.

# 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_expectation() click to toggle source

Public: Allows to write complex nested assertions.

# File lib/capybara_test_helpers/assertions.rb, line 34
def invert_expectation
  should(!not_to)
end
not_to() click to toggle source

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

# 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
Also aliased as: or_should_not
or_should_not()
Alias for: not_to
should(negated = false) click to toggle source

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

# 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
Also aliased as: should_alias
should_alias(negated = false)
Alias for: should
should_not() click to toggle source

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

# File lib/capybara_test_helpers/assertions.rb, line 20
def should_not
  @negated ? self : should(true)
end
Also aliased as: should_alias

Private Instance Methods

method_missing(method, *args, **kwargs, &block) click to toggle source

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

Calls superclass method
# 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
respond_to_missing?(method, *) click to toggle source

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

Calls superclass method
# File lib/capybara_test_helpers/assertions.rb, line 128
def respond_to_missing?(method, *)
  return false if method =~ CapybaraTestHelpers::TestHelper::DYNAMIC_MATCHER_REGEX

  super
end