vereto-api/spec/requests/todos_spec.rb

116 lines
3 KiB
Ruby
Raw Normal View History

2018-02-16 11:52:56 +00:00
# spec/requests/todos_spec.rb
require 'rails_helper'
RSpec.describe 'Todos API', type: :request do
2018-02-16 15:02:03 +00:00
# add todos owner
let(:user) { create(:user) }
let!(:todos) { create_list(:todo, 10, created_by: user.id) }
2018-02-16 11:52:56 +00:00
let(:todo_id) { todos.first.id }
2018-02-16 15:02:03 +00:00
# authorize request
let(:headers) { valid_headers }
2018-02-16 11:52:56 +00:00
# Test suite for GET /todos
describe 'GET /todos' do
# make HTTP get request before each example
2018-02-16 15:02:03 +00:00
before { get '/todos', params: {}, headers: headers }
2018-02-16 11:52:56 +00:00
it 'returns todos' do
# Note `json` is a custom helper to parse JSON responses
expect(json).not_to be_empty
expect(json.size).to eq(10)
end
it 'returns status code 200' do
expect(response).to have_http_status(200)
end
end
# Test suite for GET /todos/:id
describe 'GET /todos/:id' do
2018-02-16 15:02:03 +00:00
before { get "/todos/#{todo_id}", params: {}, headers: headers }
2018-02-16 11:52:56 +00:00
context 'when the record exists' do
it 'returns the todo' do
expect(json).not_to be_empty
expect(json['id']).to eq(todo_id)
end
it 'returns status code 200' do
expect(response).to have_http_status(200)
end
end
context 'when the record does not exist' do
let(:todo_id) { 100 }
it 'returns status code 404' do
expect(response).to have_http_status(404)
end
it 'returns a not found message' do
expect(response.body).to match(/Couldn't find Todo/)
end
end
end
# Test suite for POST /todos
describe 'POST /todos' do
# valid payload
2018-02-16 15:02:03 +00:00
let(:valid_attributes) do
# send json payload
{ title: 'Learn Elm', created_by: user.id.to_s }.to_json
end
2018-02-16 11:52:56 +00:00
context 'when the request is valid' do
2018-02-16 15:02:03 +00:00
before { post '/todos', params: valid_attributes, headers: headers }
2018-02-16 11:52:56 +00:00
it 'creates a todo' do
expect(json['title']).to eq('Learn Elm')
end
it 'returns status code 201' do
expect(response).to have_http_status(201)
end
end
context 'when the request is invalid' do
2018-02-16 15:02:03 +00:00
let(:invalid_attributes) { { title: nil }.to_json }
before { post '/todos', params: invalid_attributes, headers: headers }
2018-02-16 11:52:56 +00:00
it 'returns status code 422' do
expect(response).to have_http_status(422)
end
it 'returns a validation failure message' do
2018-02-16 15:02:03 +00:00
expect(json['message'])
.to match(/Validation failed: Title can't be blank/)
2018-02-16 11:52:56 +00:00
end
end
end
# Test suite for PUT /todos/:id
describe 'PUT /todos/:id' do
2018-02-16 15:02:03 +00:00
let(:valid_attributes) { { title: 'Shopping' }.to_json }
2018-02-16 11:52:56 +00:00
context 'when the record exists' do
2018-02-16 15:02:03 +00:00
before { put "/todos/#{todo_id}", params: valid_attributes, headers: headers }
2018-02-16 11:52:56 +00:00
it 'updates the record' do
expect(response.body).to be_empty
end
it 'returns status code 204' do
expect(response).to have_http_status(204)
end
end
end
# Test suite for DELETE /todos/:id
describe 'DELETE /todos/:id' do
2018-02-16 15:02:03 +00:00
before { delete "/todos/#{todo_id}", params: {}, headers: headers }
2018-02-16 11:52:56 +00:00
it 'returns status code 204' do
expect(response).to have_http_status(204)
end
end
end