From 1cd098e291367cebc52e5a09f3e2e6fb7646de4e Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 21 Nov 2014 16:57:33 -0700 Subject: add specs for categories controller. --- app/controllers/api/v2/categories_controller.rb | 2 +- app/views/api/v2/categories/index.json.jbuilder | 3 +++ config/routes.rb | 2 +- .../api/v2/categories_controller_spec.rb | 26 ++++++++++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 app/views/api/v2/categories/index.json.jbuilder create mode 100644 spec/controllers/api/v2/categories_controller_spec.rb diff --git a/app/controllers/api/v2/categories_controller.rb b/app/controllers/api/v2/categories_controller.rb index 86454855..365544b6 100644 --- a/app/controllers/api/v2/categories_controller.rb +++ b/app/controllers/api/v2/categories_controller.rb @@ -2,7 +2,7 @@ module Api module V2 class CategoriesController < ApplicationController def show - @category = Category.find(params[:id]) + @category = @categories.find(params[:id]) end end end diff --git a/app/views/api/v2/categories/index.json.jbuilder b/app/views/api/v2/categories/index.json.jbuilder new file mode 100644 index 00000000..4cec0c2e --- /dev/null +++ b/app/views/api/v2/categories/index.json.jbuilder @@ -0,0 +1,3 @@ +json.categories @categories do |category| + json.partial! category, category: category +end diff --git a/config/routes.rb b/config/routes.rb index 2891b953..28506416 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -71,7 +71,7 @@ Cake::Application.routes.draw do resources :cakes, only: [:index, :show] resources :photos, only: [:index, :show] resources :users, only: [:show] - resources :categories, only: [:show] + resources :categories, only: [:index, :show] end end diff --git a/spec/controllers/api/v2/categories_controller_spec.rb b/spec/controllers/api/v2/categories_controller_spec.rb new file mode 100644 index 00000000..5a913145 --- /dev/null +++ b/spec/controllers/api/v2/categories_controller_spec.rb @@ -0,0 +1,26 @@ +require 'rails_helper' + +module Api + module V2 + describe CategoriesController do + describe "#index" do + let!(:category) { create(:category) } + + it 'loads all the categories' do + xhr :get, :index + expect(assigns(:categories)).to match_array([category]) + end + end + + describe "#show" do + let!(:other_category) { create(:category) } + let!(:category) { create(:category) } + + it 'loads the specified category' do + xhr :get, :show, id: category.id + expect(assigns(:category)).to eql(category) + end + end + end + end +end -- cgit v1.2.3