OSDN Git Service

add test
[pettanr/pettanr.git] / spec / controllers / artists_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #絵師
4
5 describe ArtistsController do
6   before do
7     Factory :admin
8     @user = Factory( :user_yas)
9     @author = @user.author
10     @artist = Factory :artist_yas, :author_id => @author.id
11   end
12   
13   describe '一覧表示に於いて' do
14     before do
15       Artist.stub(:list).and_return([@artist, @artist, @artist])
16       sign_in @user
17     end
18     context '事前チェックする' do
19       it '与えられたpageがセットされている' do
20         get :index, :page => 5
21         assigns(:page).should eq 5
22       end
23       it '省略されると@pageに1値が入る' do
24         get :index
25         assigns(:page).should eq 1
26       end
27       it '与えられたpage_sizeがセットされている' do
28         get :index, :page_size => 15
29         assigns(:page_size).should eq 15
30       end
31       it '省略されると@page_sizeにデフォルト値が入る' do
32         get :index
33         assigns(:page_size).should eq Artist.default_page_size
34       end
35       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
36         get :index, :page_size => 1500
37         assigns(:page_size).should eq Artist.max_page_size
38       end
39       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
40         get :index, :page_size => 0
41         assigns(:page_size).should eq Artist.default_page_size
42       end
43     end
44     context 'つつがなく終わるとき' do
45       it 'ステータスコード200 OKを返す' do
46         get :index
47         response.should be_success 
48       end
49       it '絵師モデルに一覧を問い合わせている' do
50         Artist.should_receive(:list).exactly(1)
51         get :index
52       end
53       it '@artistsにリストを取得している' do
54         get :index
55         assigns(:artists).should have_at_least(3).items
56       end
57       context 'html形式' do
58         it 'indexテンプレートを描画する' do
59           get :index
60           response.should render_template("index")
61         end
62       end
63       context 'json形式' do
64         it 'jsonデータを返す' do
65           get :index, :format => :json
66           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
67         end
68         it 'データがリスト構造になっている' do
69           get :index, :format => :json
70           json = JSON.parse response.body
71           json.should have_at_least(3).items
72         end
73         it 'リストの先頭くらいは絵師っぽいものであって欲しい' do
74           get :index, :format => :json
75           json = JSON.parse response.body
76           json.first.has_key?("name").should be_true
77         end
78       end
79     end
80     context '作家権限がないとき' do
81       before do
82         sign_out @user
83       end
84       context 'html形式' do
85         it 'ステータスコード302 Foundを返す' do
86           get :index
87           response.status.should eq 302
88         end
89         it 'サインインページへ遷移する' do
90           get :index
91           response.should redirect_to '/users/sign_in'
92         end
93       end
94       context 'json形式' do
95         it 'ステータスコード401 Unauthorizedを返す' do
96           get :index, :format => :json
97           response.status.should eq 401
98         end
99         it '応答メッセージにUnauthorizedを返す' do
100           get :index, :format => :json
101           response.message.should match(/Unauthorized/)
102         end
103       end
104     end
105   end
106   
107   describe '閲覧に於いて' do
108     before do
109       Artist.stub(:show).and_return(@artist)
110       sign_in @user
111     end
112     context 'つつがなく終わるとき' do
113       it 'ステータスコード200 OKを返す' do
114         get :show, :id => @artist.id
115         response.should be_success
116       end
117       it '絵師モデルに単体取得を問い合わせている' do
118         Artist.should_receive(:show).exactly(1)
119         get :show
120       end
121       it '@artistにアレを取得している' do
122         get :show, :id => @artist.id
123         assigns(:artist).should eq(@artist)
124       end
125       context 'html形式' do
126         it 'showテンプレートを描画する' do
127           get :show, :id => @artist.id
128           response.should render_template("show")
129         end
130       end
131       context 'json形式' do
132         it 'jsonデータを返す' do
133           get :show, :id => @artist.id, :format => :json
134           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
135         end
136         it 'データがアレになっている' do
137           get :show, :id => @artist.id, :format => :json
138           json = JSON.parse response.body
139           json["name"].should match(/yas/)
140         end
141       end
142     end
143     context '作家権限がないとき' do
144       before do
145         sign_out @user
146       end
147       context 'html形式' do
148         it 'ステータスコード302 Foundを返す' do
149           get :show, :id => @artist.id
150           response.status.should eq 302
151         end
152         it 'サインインページへ遷移する' do
153           get :show, :id => @artist.id
154           response.body.should redirect_to '/users/sign_in'
155         end
156       end
157       context 'json形式' do
158         it 'ステータスコード401 Unauthorizedを返す' do
159           get :show, :id => @artist.id, :format => :json
160           response.status.should eq 401
161         end
162         it '応答メッセージにUnauthorizedを返す' do
163           get :show, :id => @artist.id, :format => :json
164           response.message.should match(/Unauthorized/)
165         end
166       end
167     end
168 =begin
169     context '対象作家がないとき' do
170       context 'html形式' do
171         it '例外404 not_foundを返す' do
172           lambda{
173             get :show, :id => 0
174           }.should raise_error(ActiveRecord::RecordNotFound)
175         end
176       end
177       context 'json形式' do
178         it '例外404 not_foundを返す' do
179           lambda{ 
180             get :show, :id => 0, :format => :json
181           }.should raise_error(ActiveRecord::RecordNotFound)
182         end
183       end
184     end
185 =end
186   end
187   
188   describe '絵師数取得に於いて' do
189     before do
190       Artist.should_receive(:visible_count).and_return(3)
191 #      sign_in @user
192     end
193     context 'つつがなく終わるとき' do
194       it 'ステータスコード200 OKを返す' do
195         get :count, :format => :json
196         response.should be_success 
197       end
198       context 'json形式' do
199         it 'jsonデータを返す' do
200           get :count, :format => :json
201           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
202         end
203         it 'データがHash構造になっていてコミック数が1である' do
204           get :count, :format => :json
205           json = JSON.parse response.body
206           json["count"].should == 3
207         end
208       end
209     end
210   end
211
212 end