Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DRY JSON Parsing in Rspec Tests
# spec/support/request_helpers.rb
module Requests
  module JsonHelpers
    def json
      JSON.parse(response.body)
    end
  end
end
# spec_helper.rb
RSpec.configure do |config|
  # ... The rest of your RSpec config.

  config.include Requests::JsonHelpers, type: :request
end
# spec file
describe "..." do
  it '....' do
    get "/api/v1/..."

    # test for the 200 status-code
    expect(response).to be_success

    # check that the message attributes are the same.
    expect(json['content']).to eq(message.content) 

    # ensure that private attributes aren't serialized
    expect(json['private_attr']).to eq(nil)
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment