Skip to content

Instantly share code, notes, and snippets.

@abhinavmsra
Created July 11, 2016 09:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhinavmsra/b8876ca8b40ec1febdd5e810af807ca5 to your computer and use it in GitHub Desktop.
Save abhinavmsra/b8876ca8b40ec1febdd5e810af807ca5 to your computer and use it in GitHub Desktop.
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