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
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
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
Public: Allows to write complex nested assertions.
# File lib/capybara/compose/assertions.rb, line 39 def invert_expectation should(!not_to) end
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
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
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
Private Instance Methods
Internal: Override the method_missing
defined in RSpec::Matchers to avoid accidentally calling a predicate or has matcher instead of an assertion.
# 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
Internal: Override the method_missing
defined in RSpec::Matchers to avoid accidentally calling a predicate or has matcher instead of an assertion.
# File lib/capybara/compose/assertions.rb, line 137 def respond_to_missing?(method, *) return false if method =~ DYNAMIC_MATCHER_REGEX super end