require_relative 'core_ext/hash' require_relative 'not_found_errors' # Whether response has any element with the provided value RSpec::Matchers.define :contain_value do |expected| match do |actual| expect(actual.exchange_handler.include_value?(actual.response, expected)).to be true end failure_message do |actual| "expected that #{actual.exchange_handler.response_body(actual.response, format: :hash)} would contain value #{expected}" end end # Whether substring exists in body of response (more general than above) RSpec::Matchers.define :include_in_body do |expected| match do |actual| expect(actual.exchange_handler.include_in_body?(actual.response, expected)).to be true end failure_message do |actual| "expected that #{actual.exchange_handler.response_body(actual.response, format: :raw)} would contain value #{expected}" end end # Whether an element exists at expected xpath RSpec::Matchers.define :have_element_at_path do |xpath| match do |exchange| expect { exchange[xpath] }.to_not raise_error # Error will be raised if Path returns no value end # TODO: Would be better to print failure message failure_message do |actual| "expected that #{actual.exchange_handler.response_body(actual.response, format: :raw)} would have element at path '#{xpath}'" end end RSpec::Matchers.alias_matcher :have_element_at_xpath, :have_element_at_path RSpec::Matchers.alias_matcher :contain_key, :have_element_at_path # Whether an element at xpath (defined by key) has value (defined by value). # @param [Hash] expected_hash Xpath => Value pair (e.g. '//xmlns:GetWeatherResult' => 'Data Not Found') RSpec::Matchers.define :have_xpath_value do |expected_hash| match do |exchange| expected_hash = Hash[*expected_hash.flatten] if expected_hash.is_a?(Array) # For some reason Array was occuring expect(exchange[expected_hash.keys.first]).to eq expected_hash.values.first end failure_message do |actual| "expected that xpath '#{expected_hash.keys.first}' has value '#{expected_hash.values.first}' but was '#{actual[expected_hash.keys.first]}'" end end RSpec::Matchers.define :be_found do match do |exchange| expect(exchange.exchange_handler.found?(exchange.response)).to be true end failure_message do |exchange| "expected result #{exchange.response} to be found. Status code is #{exchange.response.code}" end end