Sha256: 154fb5fca56f2c265864641ef5f5381252c64a9d7837e9bfc88ff945b5a0c436

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

def compare_array_of_hash expected, actual
  if expected.size == actual.size then
    res = true
    actual.each_with_index do |item,index|
      res = false unless item ==  expected[index]
    end
    return res
  else
    return false
  end
end

def prepare_array data
  return (data.class == Hash)? [data] : data
end


RSpec::Matchers.define :respond_with do |options={}|
  expected = options[:data]
  match do |actual|
    result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
    result  = result[options[:root]] unless options[:root].nil?
    if result.empty? then
      false 
    else
      result = prepare_array result
      expected = prepare_array expected
      compare_array_of_hash expected,result
    end
  end
  description do
    "respond with corresponding data"
  end
  failure_message do |actual|
    result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
    result  = result[options[:root]] unless options[:root].nil?
    "expected #{actual} response body would be equal \nto  : #{expected}, \ngot : #{result}"
  end
  failure_message_when_negated do |actual|
    result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
    result  = result[options[:root]] unless options[:root].nil?
    "expected #{actual} response body would not be equal \nto  : #{expected}, \ngot : #{result}"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-rest-rspec-0.0.5 lib/rack-rest-rspec/matchers/respond_with_data.rb