OSDN Git Service

fix comic test
[pettanr/pettanr.git] / spec / controllers / comics_controller_spec.rb
1 # -*- encoding: utf-8 -*-\r
2 require 'spec_helper'
3
4 describe ComicsController do
5   before do
6     Factory :admin
7     @user = Factory :user_yas\r
8     @author = @user.author    #ユーザ作成時に連動して作成される
9   end
10   
11   describe '一覧表示に於いて' do
12     before do
13       @comic = Factory :normal_comic, :author_id => @user.author.id
14       Comic.stub(:list).and_return([@comic, @comic, @comic])
15       sign_in @user
16     end
17     context '事前チェックする' do
18       it '与えられたpageがセットされている' do
19         get :index, :page => 5
20         assigns(:page).should eq 5
21       end
22       it '省略されると@pageに1値が入る' do
23         get :index
24         assigns(:page).should eq 1
25       end
26     end
27     context 'つつがなく終わるとき' do
28       it 'ステータスコード200 OKを返す' do
29         get :index
30         response.should be_success 
31       end
32       it 'コミックモデルに一覧を問い合わせている' do
33         Comic.should_receive(:list).exactly(1)\r
34         get :index
35       end
36       it '@comicsにリストを取得している' do
37         get :index
38         assigns(:comics).should have_at_least(3).items
39       end
40       context 'html形式' do
41         it 'indexテンプレートを描画する' do
42           get :index
43           response.should render_template("index")
44         end
45       end
46       context 'json形式' do
47         it 'jsonデータを返す' do
48           get :index, :format => :json
49           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
50         end
51         it 'データがリスト構造になっている' do
52           get :index, :format => :json
53           json = JSON.parse response.body\r
54           json.should have_at_least(3).items
55         end
56         it 'リストの先頭くらいはコミックっぽいものであって欲しい' do
57           get :index, :format => :json
58           json = JSON.parse response.body
59           json.first.has_key?("title").should be_true
60         end
61       end
62     end
63     context '作家権限がないとき' do
64       before do
65         sign_out @user
66       end
67       context 'html形式' do
68         it 'ステータスコード302 Foundを返す' do
69           get :index
70           response.status.should eq 302
71         end
72         it 'サインインページへ遷移する' do
73           get :index
74           response.should redirect_to '/users/sign_in'
75         end
76       end
77       context 'json形式' do
78         it 'ステータスコード401 Unauthorizedを返す' do
79           get :index, :format => :json
80           response.status.should eq 401
81         end
82         it '応答メッセージにUnauthorizedを返す' do
83           get :index, :format => :json\r
84           response.message.should match(/Unauthorized/)
85         end
86       end
87     end
88   end
89   
90   describe '単体表示に於いて' do
91     before do
92       @comic = Factory :normal_comic, :author_id => @user.author.id
93       Comic.stub(:show).and_return(@comic)
94       sign_in @user
95     end
96     context 'つつがなく終わるとき' do
97       it 'ステータスコード200 OKを返す' do
98         get :show, :id => @comic.id
99         response.should be_success
100       end
101       it 'コミックモデルに単体取得を問い合わせている' do
102         Comic.should_receive(:show).exactly(1)\r
103         get :show
104       end
105       it '@comicにアレを取得している' do
106         get :show, :id => @comic.id
107         assigns(:comic).id.should eq(@comic.id)
108       end
109       context 'html形式' do
110         it 'showテンプレートを描画する' do
111           get :show, :id => @comic.id
112           response.should render_template("show")
113         end
114       end
115       context 'json形式' do
116         it 'jsonデータを返す' do
117           get :show, :id => @comic.id, :format => :json
118           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
119         end
120         it 'データがアレになっている' do
121           get :show, :id => @comic.id, :format => :json
122           json = JSON.parse response.body
123           json["title"].should match(/normal/)
124         end
125       end
126     end
127     context '作家権限がないとき' do
128       before do
129         sign_out @user
130       end
131       context 'html形式' do
132         it 'ステータスコード302 Foundを返す' do
133           get :show, :id => @comic.id
134           response.status.should eq 302
135         end
136         it 'サインインページへ遷移する' do
137           get :show, :id => @comic.id
138           response.body.should redirect_to '/users/sign_in'
139         end
140       end
141       context 'json形式' do
142         it 'ステータスコード401 Unauthorizedを返す' do
143           get :show, :id => @comic.id, :format => :json
144           response.status.should eq 401
145         end
146         it '応答メッセージにUnauthorizedを返す' do
147           get :show, :id => @comic.id, :format => :json
148           response.message.should match(/Unauthorized/)
149         end
150       end
151     end
152 =begin
153     context '対象コミックがないとき' do
154       context 'html形式' do
155         it '例外404 not_foundを返す' do
156           lambda{\r
157             get :show, :id => 0
158           }.should raise_error(ActiveRecord::RecordNotFound)
159         end
160       end
161       context 'json形式' do
162         it '例外404 not_foundを返す' do
163           lambda{ \r
164             get :show, :id => 0, :format => :json
165           }.should raise_error(ActiveRecord::RecordNotFound)
166         end
167       end
168     end
169     context '非公開コミックを見ようとしたとき' do
170       context 'html形式' do
171         it '例外403 forbiddenを返す' do
172           Comic.any_instance.stub(:visible?).with(any_args()).and_return(false)
173           hidden = Factory :hidden_comic, :author_id => @author.id
174           lambda{\r
175             get :show, :id => hidden
176           }.should raise_error(ActiveRecord::Forbidden)
177         end
178       end
179       context 'json形式' do
180         it '例外403 forbiddenを返す' do
181           Comic.any_instance.stub(:visible?).with(any_args()).and_return(false)
182           hidden = Factory :hidden_comic, :author_id => @author.id
183           lambda{\r
184             get :show, :id => hidden, :format => :json
185           }.should raise_error(ActiveRecord::Forbidden)
186         end
187       end
188     end
189 =end
190   end
191
192   describe 'コミック数取得に於いて' do
193     before do
194       Comic.should_receive(:visible_count).and_return(3)
195 #      sign_in @user
196     end
197     context 'つつがなく終わるとき' do
198       it 'ステータスコード200 OKを返す' do
199         get :count, :format => :json
200         response.should be_success 
201       end
202       context 'json形式' do
203         it 'jsonデータを返す' do
204           get :count, :format => :json
205           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
206         end
207         it 'データがHash構造になっていてコミック数が1である' do
208           get :count, :format => :json
209           json = JSON.parse response.body
210           json["count"].should == 3
211         end
212       end
213     end
214   end
215
216   describe '新規作成フォーム表示に於いて' do
217     before do
218       sign_in @user
219     end
220     context 'つつがなく終わるとき' do
221       it 'ステータスコード200 OKを返す' do
222         get :new
223         response.should be_success 
224       end
225       it '@comicに新規データを用意している' do
226         get :new
227         assigns(:comic).should be_a_new(Comic)
228       end
229       it 'コミックモデルにデフォルト値補充を依頼している' do
230         Comic.any_instance.should_receive(:supply_default).exactly(1)\r
231         get :new
232       end
233       context 'html形式' do
234         it 'newテンプレートを描画する' do
235           get :new
236           response.should render_template("new")
237         end
238       end
239       context 'js形式' do
240         it 'new.jsテンプレートを描画する' do
241           get :new, :format => :js
242           response.should render_template("new")
243         end
244       end
245     end
246     context '作家権限がないとき' do
247       before do
248         sign_out @user
249       end
250       context 'html形式' do
251         it 'ステータスコード302 Foundを返す' do
252           get :new
253           response.status.should eq 302
254         end
255         it 'サインインページへ遷移する' do
256           get :new
257           response.body.should redirect_to '/users/sign_in'
258         end
259       end
260       context 'js形式' do
261         it 'ステータスコード401 Unauthorizedを返す' do
262           get :new, :format => :js
263           response.status.should eq 401
264         end
265         it '応答メッセージにUnauthorizedを返す' do
266           get :new, :format => :js
267           response.message.should match(/Unauthorized/)
268         end
269       end
270     end
271   end
272
273   describe '新規作成に於いて' do
274     before do
275       sign_in @user
276     end
277     context 'つつがなく終わるとき' do
278       it 'モデルに保存依頼する' do
279         Comic.any_instance.should_receive(:save).exactly(1)
280         post :create, :comic => Factory.attributes_for(:normal_comic)
281       end
282       it "@comicに作成されたコミックを保持していて、それがDBにある" do
283         post :create, :comic => Factory.attributes_for(:normal_comic)
284         assigns(:comic).should be_a(Comic)
285         assigns(:comic).should be_persisted
286       end
287       context 'html形式' do
288         it 'ステータスコード302 Foundを返す' do
289           Comic.any_instance.stub(:save).and_return(true)
290           post :create, :comic => Factory.attributes_for(:normal_comic)
291           response.status.should eq 302
292         end
293         it '作成されたコミックの表示ページへ遷移する' do
294 #          Comic.any_instance.stub(:save).and_return(true)
295           post :create, :comic => Factory.attributes_for(:normal_comic)
296           response.should redirect_to(Comic.last)
297         end
298       end
299       context 'json形式' do
300         it 'ステータスコード200 OKを返す' do
301 #          Comic.any_instance.stub(:save).and_return(true)
302           post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
303           response.should be_success 
304         end
305         it '作成されたコミックをjsonデータで返す' do
306           post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
307           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
308         end
309         it 'データがアレになっている' do
310           post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
311           json = JSON.parse response.body
312           json["title"].should match(/normal/)
313         end
314       end
315     end
316     context '作家権限がないとき' do
317       before do
318         sign_out @user
319       end
320       context 'html形式' do
321         it 'ステータスコード302 Foundを返す' do
322           post :create, :comic => Factory.attributes_for(:normal_comic)
323           response.status.should eq 302
324         end
325         it 'サインインページへ遷移する' do
326           post :create, :comic => Factory.attributes_for(:normal_comic)
327           response.body.should redirect_to '/users/sign_in'
328         end
329       end
330       context 'json形式' do
331         it 'ステータスコード401 Unauthorizedを返す' do
332           post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
333           response.status.should eq 401
334         end
335         it '応答メッセージにUnauthorizedを返す' do
336           post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
337           response.message.should match(/Unauthorized/)
338         end
339       end
340     end
341     context '検証、保存に失敗した' do
342       before do
343         Comic.any_instance.stub(:save).and_return(false)
344       end
345       it "未保存のコミックを保持している" do
346         post :create, :comic => {}
347         assigns(:comic).should be_a_new(Comic)
348       end
349       context 'html形式' do
350         it 'ステータスコード200 OKを返す' do
351           post :create, :comic => {}
352           response.status.should eq 200
353         end
354         it '新規ページを描画する' do
355           post :create, :comic => {}
356           response.should render_template("new")
357         end
358       end
359       context 'json形式' do
360         it 'ステータスコード422 unprocessable_entity を返す' do
361           post :create, :comic => {}, :format => :json
362           response.status.should eq 422
363         end
364         it '応答メッセージUnprocessable Entityを返す' do
365           post :create, :comic => {}, :format => :json
366           response.message.should match(/Unprocessable/)
367         end
368       end
369     end
370   end
371
372   describe '編集フォーム表示に於いて' do
373     before do
374       @comic = Factory :normal_comic, :author_id => @user.author.id
375       sign_in @user
376       Comic.stub(:show).and_return(@comic)
377     end
378     context 'つつがなく終わるとき' do
379       it 'ステータスコード200 OKを返す' do
380         get :edit, :id => @comic.id
381         response.should be_success 
382       end
383       it 'コミックモデルに単体取得を問い合わせている' do
384         Comic.should_receive(:show).exactly(1)\r
385         get :edit, :id => @comic.id
386       end
387       it '@comicにデータを用意している' do
388         get :edit, :id => @comic.id
389         assigns(:comic).should eq @comic
390       end
391       context 'html形式' do
392         it 'editテンプレートを描画する' do
393           get :edit, :id => @comic.id
394           response.should render_template("edit")
395         end
396       end
397       context 'js形式' do
398         it 'edit.jsテンプレートを描画する' do
399           get :edit, :id => @comic.id, :format => :js
400           response.should render_template("edit")
401         end
402       end
403     end
404     context '作家権限がないとき' do
405       before do
406         sign_out @user
407       end
408       context 'html形式' do
409         it 'ステータスコード302 Foundを返す' do
410           get :edit, :id => @comic.id
411           response.status.should eq 302
412         end
413         it 'サインインページへ遷移する' do
414           get :edit, :id => @comic.id
415           response.body.should redirect_to '/users/sign_in'
416         end
417       end
418       context 'js形式' do
419         it 'ステータスコード401 Unauthorizedを返す' do
420           get :edit, :id => @comic.id, :format => :js
421           response.status.should eq 401
422         end
423         it '応答メッセージにUnauthorizedを返す' do
424           get :edit, :id => @comic.id, :format => :js
425           response.message.should match(/Unauthorized/)
426         end
427       end
428     end
429   end
430
431   describe '更新に於いて' do
432     before do\r
433       @comic = Factory :normal_comic, :author => @author
434       sign_in @user
435     end
436     context '事前チェックしておく' do
437       it 'コミックモデルに単体取得を問い合わせている' do
438         Comic.stub(:show).with(any_args()).and_return @comic\r
439         Comic.should_receive(:show).exactly(1)\r
440         put :update, :id => @comic.id, :comic => Factory.attributes_for(:normal_comic)
441       end
442       it 'モデルに更新を依頼する' do
443         Comic.any_instance.should_receive(:update_attributes).with(any_args)
444         put :update, :id => @comic.id, :comic => Factory.attributes_for(:normal_comic)
445       end
446       it '@comicにアレを取得している' do
447         put :update, :id => @comic.id, :comic => Factory.attributes_for(:normal_comic)
448         assigns(:comic).id.should eq(@comic.id)
449       end
450     end
451     context 'つつがなく終わるとき' do
452       it '更新される' do\r
453         put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)\r
454         Comic.find(@comic.id).visible.should eq 0
455       end
456       context 'html形式' do
457         it 'ステータスコード302 Foundを返す' do
458           Comic.any_instance.stub(:update_attributes).with(any_args()).and_return(true)
459           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
460           response.status.should eq 302
461         end
462         it '更新されたコミックの表示ページへ遷移する' do
463           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
464           response.should redirect_to(@comic)
465         end
466       end
467       context 'json形式' do
468         it 'ステータスコード200 OKを返す' do
469           Comic.any_instance.stub(:update_attributes).with(any_args()).and_return(true)
470           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
471           response.should be_success 
472         end
473         it 'ページ本体は特に返さない' do
474           Comic.any_instance.stub(:update_attributes).with(any_args()).and_return(true)
475           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
476           response.body.should match /./
477         end
478       end
479     end
480     context '作家権限がないとき' do
481       before do
482         sign_out @user
483       end
484       it 'ステータスコード302 Foundを返す' do
485         put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
486         response.status.should eq 302
487       end
488       context 'html形式' do
489         it 'サインインページへ遷移する' do
490           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
491           response.body.should redirect_to '/users/sign_in'
492         end
493       end
494       context 'json形式' do
495         it '応答メッセージにUnauthorizedを返す' do
496           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
497           response.message.should match(/Unauthorized/)
498         end
499       end
500     end
501     context '検証、保存に失敗したとき' do
502       before do
503         Comic.any_instance.stub(:update_attributes).and_return(false)
504       end
505       context 'html形式' do
506         it 'ステータスコード200 Okを返す' do
507           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
508           response.status.should eq 200
509         end
510         it '編集ページを描画する' do
511           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
512           response.should render_template("edit")
513         end
514       end
515       context 'json形式' do
516         it 'ステータスコード422 unprocessable_entity を返す' do
517           Comic.any_instance.stub(:update_attributes).and_return(false)
518           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
519           response.status.should eq 422
520         end
521         it '応答メッセージUnprocessable Entityを返す' do
522           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
523           response.message.should match(/Unprocessable/)
524         end
525       end
526     end
527   end
528
529
530 end