Skip to content

Instantly share code, notes, and snippets.

@dpaluy
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpaluy/b43821d086211bb10cda to your computer and use it in GitHub Desktop.
Save dpaluy/b43821d086211bb10cda to your computer and use it in GitHub Desktop.
JSON Helper for API specs
# spec/requests/api/v1/messages_spec.rb
describe "Messages API", type: :request do
it 'retrieves a specific message' do
message = FactoryGirl.create(:message)
get "/api/v1/messages/#{message.id}"
# 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
# spec/support/request_helpers.rb
module Requests
module JsonHelpers
def json
@json ||= JSON.parse(response.body)
end
end
end
RSpec.configure do |config|
config.include Requests::JsonHelpers, type: :request
end
@dpaluy
Copy link
Author

dpaluy commented Jul 27, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment