Sha256: 7b7fb41795291abc3c6fb3996781f7b08abd5dc915777ef8fb87c24e39ae0492

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

require 'rapporteur'

shared_examples_for 'a successful status response' do
  let(:parsed_body) { JSON.parse(response.body) }

  it 'responds with HTTP 200' do
    expect(response.response_code).to(eq(200))
  end

  it 'responds with a JSON content header' do
    expect(response.content_type).to(eq(Mime[:json]))
  end

  context 'the response payload' do
    it 'does not contain errors' do
      expect(parsed_body).not_to(have_key('errors'))
    end
  end
end

shared_examples_for 'an erred status response' do
  let(:parsed_body) { JSON.parse(response.body) }

  it 'responds with HTTP 500' do
    expect(response.response_code).to(eq(500))
  end

  it 'responds with a JSON content header' do
    expect(response.content_type).to(eq(Mime[:json]))
  end

  it 'contains errors' do
    expect(parsed_body).to(have_key('errors'))
    expect(parsed_body.fetch('errors')).not_to(be_empty)
  end
end

RSpec::Matchers.define :include_status_error_message do |attribute, message|
  match do |response|
    @body = JSON.parse(response.body)
    @body.fetch('errors', {}).fetch(attribute.to_s).match(message)
  end

  failure_message_for_should do |actual|
    "expected #{@body.inspect} to include a #{attribute}:#{message.inspect} error message"
  end

  failure_message_for_should_not do |actual|
    "expected #{@body.inspect} to not include a #{attribute}:#{message.inspect} error message"
  end
end

RSpec::Matchers.define :include_status_message do |name, message|
  match do |response|
    @body = JSON.parse(response.body)
    @body.has_key?(name) && @body.fetch(name).match(message)
  end

  failure_message_for_should do |actual|
    "expected #{@body.inspect} to include a #{name.inspect}: #{message.inspect} message"
  end

  failure_message_for_should_not do |actual|
    "expected #{@body.inspect} to not include a #{name.inspect}: #{message.inspect} message"
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rapporteur-3.5.1 lib/rapporteur/rspec.rb
rapporteur-3.5.0 lib/rapporteur/rspec.rb