OSDN Git Service

7ad3e504cc4a6f217ed7fbdb92625864edfea2ff
[pettanr/pettanr.git] / spec / controllers / colors_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #色
4
5 describe ColorsController do
6   before do
7     @admin = FactoryGirl.create :admin
8     @user = FactoryGirl.create( :user_yas)
9     @author = FactoryGirl.create :author, :user_id => @user.id
10     @sp = FactoryGirl.create :system_picture
11     @lg = FactoryGirl.create :license_group, :name => 'peta'
12     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
13   end
14
15   describe '一覧表示に於いて' do
16     before do
17       sign_in @user
18       @color = FactoryGirl.create :color
19       Color.stub(:list).and_return([@color, @color, @color])
20     end
21     context 'パラメータpageについて' do
22       it '@pageに値が入る' do
23         get :index, :page => 5
24         assigns(:page).should eq 5
25       end
26       it '省略されると@pageに1値が入る' do
27         get :index
28         assigns(:page).should eq 1
29       end
30       it '与えられたpage_sizeがセットされている' do
31         get :index, :page_size => 15
32         assigns(:page_size).should eq 15
33       end
34       it '省略されると@page_sizeにデフォルト値が入る' do
35         get :index
36         assigns(:page_size).should eq Color.default_page_size
37       end
38       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
39         get :index, :page_size => 1500
40         assigns(:page_size).should eq Color.max_page_size
41       end
42       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
43         get :index, :page_size => 0
44         assigns(:page_size).should eq Color.default_page_size
45       end
46     end
47     context 'つつがなく終わるとき' do
48       it 'ステータスコード200 OKを返す' do
49         get :index
50         response.should be_success 
51       end
52       it '色モデルに一覧を問い合わせている' do
53         Color.should_receive(:list).exactly(1)
54         get :index
55       end
56       it '@colorsにリストを取得している' do
57         get :index
58         assigns(:colors).should have_at_least(3).items
59       end
60       context 'html形式' do
61         it 'indexテンプレートを描画する' do
62           get :index
63           response.should render_template("index")
64         end
65       end
66       context 'json形式' do
67         it 'jsonデータを返す' do
68           get :index, :format => :json
69           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
70         end
71         it '色モデルにjson一覧出力オプションを問い合わせている' do
72           Color.should_receive(:list_json_opt).exactly(1)
73           get :index, :format => :json
74         end
75         it 'データがリスト構造になっている' do
76           get :index, :format => :json
77           json = JSON.parse response.body
78           json.should have_at_least(3).items
79         end
80         it 'リストの先頭くらいは色っぽいものであって欲しい' do
81           get :index, :format => :json
82           json = JSON.parse response.body
83           json.first.has_key?("name").should be_true
84           json.first.has_key?("code").should be_true
85           json.first.has_key?("t").should be_true
86         end
87       end
88     end
89     context '作家権限がないとき' do
90       before do
91         sign_out @user
92       end
93       it 'ステータスコード200 okを返す' do
94         get :index
95         response.status.should eq 200
96       end
97     end
98   end
99   
100   describe '単体表示に於いて' do
101     before do
102       sign_in @user
103       @color = FactoryGirl.create :color
104       Color.stub(:show).and_return(@color)
105     end
106     context 'つつがなく終わるとき' do
107       it 'ステータスコード200 OKを返す' do
108         get :show, :id => @color.id
109         response.should be_success
110       end
111       it '色モデルに単体取得を問い合わせている' do
112         Color.should_receive(:show).exactly(1)
113         get :show
114       end
115       it '@colorにアレを取得している' do
116         get :show, :id => @color.id
117         assigns(:color).id.should eq(@color.id)
118       end
119       context 'html形式' do
120         it 'showテンプレートを描画する' do
121           get :show, :id => @color.id
122           response.should render_template("show")
123         end
124       end
125       context 'json形式' do
126         it 'jsonデータを返す' do
127           get :show, :id => @color.id, :format => :json
128           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
129         end
130         it '色モデルにjson単体出力オプションを問い合わせている' do
131           Color.should_receive(:show_json_opt).exactly(1)
132           get :show, :id => @color.id, :format => :json
133         end
134         it 'データがアレになっている' do
135           get :show, :id => @color.id, :format => :json
136           json = JSON.parse response.body
137           json["name"].should_not be_nil
138           json["code"].should_not be_nil
139           json["t"].should_not be_nil
140         end
141       end
142     end
143     context '作家権限がないとき' do
144       before do
145         sign_out @user
146       end
147       it 'ステータスコード200 okを返す' do
148         get :show, :id => @color.id
149         response.status.should eq 200
150       end
151     end
152   end
153   
154 end