Last active
August 29, 2015 14:26
-
-
Save dpaluy/b43821d086211bb10cda to your computer and use it in GitHub Desktop.
JSON Helper for API specs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/support/request_helpers.rb | |
module Requests | |
module JsonHelpers | |
def json | |
@json ||= JSON.parse(response.body) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.configure do |config| | |
config.include Requests::JsonHelpers, type: :request | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read more: http://matthewlehner.net/rails-api-testing-guidelines/