Skip to content

Commit

Permalink
add items controller with spec
Browse files Browse the repository at this point in the history
  • Loading branch information
ChakmaSajib committed Dec 19, 2021
1 parent 4288ac5 commit daede21
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 4 deletions.
39 changes: 39 additions & 0 deletions app/controllers/items_controller.rb
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
69 changes: 65 additions & 4 deletions spec/requests/items_spec.rb
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

0 comments on commit daede21

Please sign in to comment.