Sha256: d45011ecd21323dab92735d23598bb41dbfa5bc954aa2961b8b025315f5d21ee

Contents?: true

Size: 1.94 KB

Versions: 10

Compression:

Stored size: 1.94 KB

Contents

shared_examples_for "a JSON 404 response" do
  it "returns a 404 Not Found" do
    subject
    expect(last_response.status).to eq 404
  end
end

shared_examples_for "a 200 JSON response" do

end

require 'rspec/expectations'

RSpec::Matchers.define :be_a_hal_json_success_response do
  match do | actual |
    expect(actual.status).to be 200
    expect(actual.headers['Content-Type']).to eq 'application/hal+json;charset=utf-8'
  end

  failure_message do
    "Expected successful json response, got #{actual.status} #{actual.headers['Content-Type']} with body #{actual.body}"
  end
end

RSpec::Matchers.define :be_a_json_response do
  match do | actual |
    expect(actual.headers['Content-Type']).to eq 'application/json;charset=utf-8'
  end
end

RSpec::Matchers.define :be_a_json_error_response do | message |
  match do | actual |
    expect(actual.status).to be 400
    expect(actual.headers['Content-Type']).to eq 'application/json;charset=utf-8'
    expect(actual.body).to include message
  end
end

RSpec::Matchers.define :be_a_404_response do
  match do | actual |
    expect(actual.status).to be 404
  end
end

RSpec::Matchers.define :include_hashes_matching do |expected_array_of_hashes|
  match do |array_of_hashes|
    expected_array_of_hashes.each do | expected |
      expect(array_of_hashes).to include_hash_matching(expected)
    end

    expect(array_of_hashes.size).to eq expected_array_of_hashes.size
  end

  def slice actual, keys
    keys.each_with_object({}) { |k, hash| hash[k] = actual[k] if actual.has_key?(k) }
  end
end

RSpec::Matchers.define :include_hash_matching do |expected|
  match do |array_of_hashes|
    @array_of_hashes = array_of_hashes
    array_of_hashes.any? { |actual| slice(actual, expected.keys) == expected }
  end

  failure_message do
    "expected #{@array_of_hashes.inspect} to include #{expected.inspect}"
  end

  def slice actual, keys
    keys.each_with_object({}) { |k, hash| hash[k] = actual[k] if actual.has_key?(k) }
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pact_broker-2.15.0 spec/support/shared_examples_for_responses.rb
pact_broker-2.14.0 spec/support/shared_examples_for_responses.rb
pact_broker-2.13.1 spec/support/shared_examples_for_responses.rb
pact_broker-2.13.0 spec/support/shared_examples_for_responses.rb
pact_broker-2.12.0 spec/support/shared_examples_for_responses.rb
pact_broker-2.11.0 spec/support/shared_examples_for_responses.rb
pact_broker-2.10.0 spec/support/shared_examples_for_responses.rb
pact_broker-2.9.0 spec/support/shared_examples_for_responses.rb
pact_broker-2.9.0.beta.5 spec/support/shared_examples_for_responses.rb
pact_broker-2.8.0.beta.5 spec/support/shared_examples_for_responses.rb