-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4288ac5
commit daede21
Showing
2 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,41 @@ | ||
class ItemsController < ApplicationController | ||
before_action :set_todo | ||
before_action :set_todo_item, only: [:show, :update, :destroy] | ||
|
||
# GET /todos/:todo_id/items | ||
def index | ||
json_response(@todo.items) | ||
end | ||
|
||
# GET /todos/:todo_id/items/:id | ||
def show | ||
json_response(@item) | ||
end | ||
|
||
# POST /todos/:todo_id/items | ||
def create | ||
@todo.items.create!(item_params) | ||
json_response(@todo, :created) | ||
end | ||
|
||
# PUT /todos/:todo_id/items/:id | ||
def destroy | ||
@item.destroy | ||
head :no_content | ||
end | ||
|
||
|
||
private | ||
|
||
def item_params | ||
params.permit(:name, :done) | ||
end | ||
|
||
def set_todo | ||
@todo = Todo.find(params[:id]) | ||
end | ||
|
||
def set_todo_item | ||
@item = @todo.items.find_by!(id: params[:id]) if @todo | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,68 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "Items", type: :request do | ||
describe "GET /index" do | ||
pending "add some examples (or delete) #{__FILE__}" | ||
RSpec.describe "Items API" do | ||
#Initialize the test data | ||
let!(:todo) { create(:todo) } | ||
let!(:items) { create_list(:item, 20, todo_id: todo.id) } | ||
let(:todo_id) { todo.id } | ||
let(:id) { items.first.id } | ||
|
||
# Test suite for GET /todos/:todo_id/items | ||
describe "GET /todos/:todo_id/items" do | ||
before { get "/todo/#{todo_id}/items" } | ||
|
||
context 'when todo exists' do | ||
it 'returns status code 200' do | ||
expect(response).to have_http_status(200) | ||
end | ||
|
||
it 'returns all todo items' do | ||
expect(json.size).to eq(20) | ||
end | ||
end | ||
context 'when todo does not exist' do | ||
let(:todo_id) { 0 } | ||
|
||
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 PUT /todos/:todo_id/items | ||
describe 'POST /todos/:todo_id/items' do | ||
let(:valid_attributes) { { name: "visit narnia", done: false } } | ||
|
||
context "when request attributes are valid" do | ||
before { post "/todos/#{todo_id}/items", params: valid_attributes } | ||
|
||
it "returns status code 201" do | ||
expect(response).to have_http_status(201) | ||
end | ||
end | ||
|
||
context "when an invalid request" do | ||
before { post "/todos/{todo_id}/items", 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: Name can't be blank/) | ||
end | ||
end | ||
end | ||
# Test suite for DELETE /todos/:id | ||
describe 'DELETE /todos/:id' do | ||
before { delete "/todos/#{todo_id}/items/#{id}"} | ||
|
||
it "returns status code 204" do | ||
expect(response).to have_http_status(204) | ||
end | ||
end | ||
end | ||
end |