module Capybara::Compose::Assertions

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

Constants

BE_PREDICATE_REGEX
DYNAMIC_MATCHER_REGEX
HAS_REGEX

Public Instance Methods

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

Public: Allows to call have_selector with a shorter syntax.

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

Public: Allows to check on any input value asynchronously.

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

Public: Allows to write complex nested assertions.

# File lib/capybara/compose/assertions.rb, line 39
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/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
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/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
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/compose/assertions.rb, line 25
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/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
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/compose/assertions.rb, line 137
def respond_to_missing?(method, *)
  return false if method =~ DYNAMIC_MATCHER_REGEX

  super
end