Commit 1cd098e2

mo khan <mo@mokhan.ca>
2014-11-21 23:57:33
add specs for categories controller.
1 parent 51265f2
Changed files (4)
app
controllers
views
api
v2
config
spec
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
app/views/api/v2/categories/index.json.jbuilder
@@ -0,0 +1,3 @@
+json.categories @categories do |category|
+  json.partial! category, category: category
+end
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
 
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