Skip to content

Instantly share code, notes, and snippets.

@floodico
Created October 25, 2017 11:42
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 floodico/ce0acf299bde71aeabb41d336a4fb3fa to your computer and use it in GitHub Desktop.
Save floodico/ce0acf299bde71aeabb41d336a4fb3fa to your computer and use it in GitHub Desktop.
require 'rails_helper'
RSpec.describe 'Comments API', type: :request do
let!(:post) { create(:post) }
let!(:comments) { create_list(:comment, 20, post_id: post.id) }
let(:post_id) { post.id }
let(:id) { comments.first.id }
describe 'POST /posts/:post_id/comments' do
let(:valid_attributes) { { body: 'Hello Body' } }
context 'when the request is valid' do
before { post "/posts/#{post_id}/comments", params: valid_attributes }
it 'creates a post' do
expect(json['body']).to eq('Great post')
end
it 'returns status code 201' do
expect(response).to have_http_status(201)
end
end
context 'when the request is invalid' do
before { post "/posts/#{post_id}/comments", params: {} }
it 'returns status code 422' do
expect(response).to have_http_status(422)
end
it 'returns a failure message' do
expect(response.body).to match(/Validation failed: Body can't be blank/)
end
end
end
end
require 'rails_helper'
RSpec.describe 'Posts API', type: :request do
let!(:posts) { create_list(:post, 10) }
let(:post_id) { posts.first.id }
# POST /posts
describe 'POST /posts' do
let(:valid_attributes) { { title: 'Hello Body', description: "Hella yella, Shrek. Lol kek cheburek. Who's lovin' weed? Pussy money weed." } }
context 'when the request is valid' do
before { post '/posts', params: valid_attributes}
it 'creates a post' do
expect(json['title']).to eq('Hello Body')
expect(json['description']).to eq("Hella yella, Shrek. Lol kek cheburek. Who's lovin' weed? Pussy money weed.")
end
it 'returns status code 201' do
expect(response).to have_http_status(201)
end
end
context 'when the request is invalid' do
before { post '/posts', params: { title: 'Hey man' } }
it 'returns status code 422' do
expect(response).to have_http_status(422)
end
it 'returns a validation failure message' do
expect(response.body)
.to match(/Validation failed: Description can't be blank/)
end
end
end
end
.....................
resources :posts do
resources :comments
end
.....................
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment