1 # -*- encoding: utf-8 -*-
\r
4 describe ComicsController do
5 # This should return the minimal set of attributes required to create a valid
6 # Comic. As you add validations to Comic, be sure to
7 # update the return value of this method accordingly.
10 @user = Factory :user_yas
\r
11 @author = @user.author #ユーザ作成時に連動して作成される
15 {:title => 'test comic', :width => 100, :height => 50, :visible => 0, :editable => 0}
18 describe '一覧表示に於いて' do
20 Factory :normal_comic, :author_id => @user.author.id
21 Factory :visible_comic, :author_id => @user.author.id
22 Factory:editable_comic, :author_id => @user.author.id
23 Factory :hidden_comic, :author_id => @user.author.id
27 it '与えられたpageがセットされている' do
28 get :index, :page => 5
29 assigns(:page).should eq 5
32 context 'つつがなく終わるとき' do
33 it 'ステータスコード200 OKを返す' do
35 response.should be_success
37 it '@comicsにリストを取得している' do
39 assigns(:comics).should have_at_least(3).items
42 it 'indexテンプレートを描画する' do
44 response.should render_template("index")
49 get :index, :format => :json
50 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
52 it 'データがリスト構造になっている' do
53 get :index, :format => :json
54 json = JSON.parse response.body
\r
55 json.should have_at_least(3).items
57 it 'リストの先頭くらいはコミックっぽいものであって欲しい' do
58 get :index, :format => :json
59 json = JSON.parse response.body
60 json.first.has_key?("title").should be_true
64 context '作家権限がないとき' do
66 it 'ステータスコード302 Foundを返す' do
69 response.status.should eq 302
74 response.should redirect_to '/users/sign_in'
78 it 'ステータスコード401 Unauthorizedを返す' do
80 get :index, :format => :json
81 response.status.should eq 401
84 get :index, :format => :json
85 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
87 it '応答メッセージにUnauthorizedを返す' do
89 get :index, :format => :json
\r
90 response.message.should match(/Unauthorized/)
96 describe '単体表示に於いて' do
98 @comic = Factory :normal_comic, :author_id => @user.author.id
101 context 'つつがなく終わるとき' do
102 it 'ステータスコード200 OKを返す' do
103 get :show, :id => @comic.id
104 response.should be_success
106 it '@comicにアレを取得している' do
107 get :show, :id => @comic.id
108 assigns(:comic).id.should eq(@comic.id)
111 it 'showテンプレートを描画する' do
112 get :show, :id => @comic.id
113 response.should render_template("show")
118 get :show, :id => @comic.id, :format => :json
119 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
122 get :show, :id => @comic.id, :format => :json
123 json = JSON.parse response.body
124 json["title"].should match(/normal/)
128 context '作家権限がないとき' do
129 it 'ステータスコード302 Foundを返す' do
131 get :show, :id => @comic.id
132 response.status.should eq 302
135 it 'サインインページへ遷移する' do
137 get :show, :id => @comic.id
138 response.body.should redirect_to '/users/sign_in'
142 it '応答メッセージにUnauthorizedを返す' do
144 get :show, :id => @comic.id, :format => :json
145 response.message.should match(/Unauthorized/)
149 context '対象コミックがないとき' do
151 it '例外404 not_foundを返す' do
154 }.should raise_error(ActiveRecord::RecordNotFound)
158 it '例外404 not_foundを返す' do
160 get :show, :id => 0, :format => :json
161 }.should raise_error(ActiveRecord::RecordNotFound)
165 context '非公開コミックを見ようとしたとき' do
167 it '例外403 forbiddenを返す' do
168 Comic.any_instance.stub(:vis).with(any_args()).and_return(false)
169 hidden = Factory :hidden_comic, :author_id => @author.id
171 get :show, :id => hidden
172 }.should raise_error(ActiveRecord::Forbidden)
176 it '例外403 forbiddenを返す' do
177 Comic.any_instance.stub(:vis).with(any_args()).and_return(false)
178 hidden = Factory :hidden_comic, :author_id => @author.id
180 get :show, :id => hidden, :format => :json
181 }.should raise_error(ActiveRecord::Forbidden)
187 describe 'コミック数取得に於いて' do
189 Comic.should_receive(:visible_count).and_return(3)
192 context 'つつがなく終わるとき' do
193 it 'ステータスコード200 OKを返す' do
194 get :count, :format => :json
195 response.should be_success
199 get :count, :format => :json
200 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
202 it 'データがHash構造になっていてコミック数が1である' do
203 get :count, :format => :json
204 json = JSON.parse response.body
205 json["count"].should == 3
211 describe '新規作成フォーム表示に於いて' do
215 context 'つつがなく終わるとき' do
216 it 'ステータスコード200 OKを返す' do
218 response.should be_success
220 it '@comicに新規データを用意している' do
222 assigns(:comic).should be_a_new(Comic)
224 it '@comicにデフォルト値セットが施されている' do
225 Comic.any_instance.should_receive(:supply_default).exactly(1)
229 it 'newテンプレートを描画する' do
231 response.should render_template("new")
235 it 'new.jsテンプレートを描画する' do
236 get :new, :format => :js
237 response.should render_template("new")
241 context '作家権限がないとき' do
243 it 'ステータスコード302 Foundを返す' do
246 response.status.should eq 302
248 it 'サインインページへ遷移する' do
251 response.body.should redirect_to '/users/sign_in'
255 it 'ステータスコード401 Unauthorizedを返す' do
257 get :new, :format => :js
258 response.status.should eq 401
260 it '応答メッセージにUnauthorizedを返す' do
262 get :new, :format => :js
263 response.message.should match(/Unauthorized/)
269 describe '新規作成に於いて' do
273 context 'つつがなく終わるとき' do
275 Comic.any_instance.should_receive(:save).exactly(1)
276 post :create, :comic => Factory.attributes_for(:normal_comic)
278 it 'ステータスコード302 Foundを返す' do
279 Comic.any_instance.stub(:save).and_return(true)
280 post :create, :comic => Factory.attributes_for(:normal_comic)
281 response.status.should eq 302
283 it "@comicに作成されたコミックを保持していて、それがDBにある" do
284 post :create, :comic => Factory.attributes_for(:normal_comic)
285 assigns(:comic).should be_a(Comic)
286 assigns(:comic).should be_persisted
289 it '作成されたコミックの表示ページへ遷移する' do
290 post :create, :comic => Factory.attributes_for(:normal_comic)
291 response.should redirect_to(Comic.last)
295 it '作成されたコミックをjsonデータで返す' do
296 post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
297 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
300 post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
301 json = JSON.parse response.body
302 json["title"].should match(/normal/)
306 context '作家権限がないとき' do
307 it 'ステータスコード302 Foundを返す' do
309 post :create, :comic => Factory.attributes_for(:normal_comic)
310 response.status.should eq 302
313 it 'サインインページへ遷移する' do
315 post :create, :comic => Factory.attributes_for(:normal_comic)
316 response.body.should redirect_to '/users/sign_in'
320 it '応答メッセージにUnauthorizedを返す' do
322 post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
323 response.message.should match(/Unauthorized/)
327 context '検証、保存に失敗した' do
328 it "未保存のコミックを保持している" do
329 Comic.any_instance.stub(:save).and_return(false)
330 post :create, :comic => {}
331 assigns(:comic).should be_a_new(Comic)
334 it 'ステータスコード200 OKを返す' do
335 Comic.any_instance.stub(:save).and_return(false)
336 post :create, :comic => {}
337 response.status.should eq 200
340 Comic.any_instance.stub(:save).and_return(false)
341 post :create, :comic => {}
342 response.should render_template("new")
346 it 'ステータスコード422 unprocessable_entity を返す' do
347 Comic.any_instance.stub(:save).and_return(false)
348 post :create, :comic => {}, :format => :json
349 response.status.should eq 422
352 Comic.any_instance.stub(:save).and_return(false)
353 post :create, :comic => {}, :format => :json
354 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
357 Comic.any_instance.stub(:save).and_return(false)
358 post :create, :comic => {}, :format => :json
359 json = JSON.parse response.body
366 describe '編集フォーム表示に於いて' do
368 @comic = Factory :normal_comic, :author_id => @user.author.id
371 context 'つつがなく終わるとき' do
372 it 'ステータスコード200 OKを返す' do
373 get :edit, :id => @comic.id
374 response.should be_success
376 it '@comicにデータを用意している' do
377 get :edit, :id => @comic.id
378 assigns(:comic).should eq @comic
380 it '@comicにデフォルト値セットが施されている' do
381 Comic.any_instance.should_receive(:supply_default).exactly(1)
382 get :edit, :id => @comic.id
\r
385 it 'editテンプレートを描画する' do
386 get :edit, :id => @comic.id
387 response.should render_template("edit")
391 it 'edit.jsテンプレートを描画する' do
392 get :edit, :id => @comic.id, :format => :js
393 response.should render_template("edit")
397 context '作家権限がないとき' do
399 it 'ステータスコード302 Foundを返す' do
401 get :edit, :id => @comic.id
402 response.status.should eq 302
404 it 'サインインページへ遷移する' do
406 get :edit, :id => @comic.id
407 response.body.should redirect_to '/users/sign_in'
411 it 'ステータスコード401 Unauthorizedを返す' do
413 get :edit, :id => @comic.id, :format => :js
414 response.status.should eq 401
416 it '応答メッセージにUnauthorizedを返す' do
418 get :edit, :id => @comic.id, :format => :js
419 response.message.should match(/Unauthorized/)
423 context '対象コミックがないとき' do
425 it '例外404 not_foundを返す' do
427 get :edit, :id => 0
\r
428 }.should raise_error(ActiveRecord::RecordNotFound)
432 it '例外404 not_foundを返す' do
434 get :edit, :id => 0, :format => :js
\r
435 }.should raise_error(ActiveRecord::RecordNotFound)
439 context '自分のコミックでないとき' do
441 it '例外403 forbiddenを返す' do
442 other = Factory :visible_comic, :author_id => 0
444 get :edit, :id => other.id
445 }.should raise_error(ActiveRecord::Forbidden)
449 it '例外403 forbiddenを返す' do
450 other = Factory :visible_comic, :author_id => 0
452 get :edit, :id => other.id
453 }.should raise_error(ActiveRecord::Forbidden)
461 @comic = Factory :normal_comic, :author => @author
464 context '事前チェックしておく' do
466 Comic.any_instance.should_receive(:update_attributes).with(any_args)
467 put :update, :id => @comic.id, :comic => Factory.attributes_for(:normal_comic)
469 it '@comicにアレを取得している' do
470 put :update, :id => @comic.id, :comic => Factory.attributes_for(:normal_comic)
471 assigns(:comic).id.should eq(@comic.id)
474 context 'つつがなく終わるとき' do
475 it 'ステータスコード302 Foundを返す' do
476 Comic.any_instance.stub(:update_attributes).with(any_args()).and_return(true)
477 put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
478 response.status.should eq 302
481 put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
\r
482 Comic.find(@comic.id).visible.should eq 0
485 it '更新されたコミックの表示ページへ遷移する' do
486 put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
487 response.should redirect_to(@comic)
492 Comic.any_instance.stub(:update_attributes).with(any_args()).and_return(true)
493 put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
494 response.body.should match /./
498 context '作家権限がないとき' do
499 it 'ステータスコード302 Foundを返す' do
501 put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
502 response.status.should eq 302
505 it 'サインインページへ遷移する' do
507 put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
508 response.body.should redirect_to '/users/sign_in'
512 it '応答メッセージにUnauthorizedを返す' do
514 put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
515 response.message.should match(/Unauthorized/)
519 context '対象コミックがないとき' do
521 it '例外404 not_foundを返す' do
523 put :update, :id => 0, :comic => Factory.attributes_for(:hidden_comic)
524 }.should raise_error(ActiveRecord::RecordNotFound)
528 it '例外404 not_foundを返す' do
530 put :update, :id => 0, :comic => Factory.attributes_for(:hidden_comic), :format => :json
531 }.should raise_error(ActiveRecord::RecordNotFound)
535 context '他人のコミックを編集しようとしたとき' do
537 it '例外403 forbiddenを返す' do
538 other = Factory :visible_comic, :author_id => 0
540 put :update, :id => other.id, :comic => Factory.attributes_for(:hidden_comic)
541 }.should raise_error(ActiveRecord::Forbidden)
545 it '例外403 forbiddenを返す' do
546 other = Factory :visible_comic, :author_id => 0
548 put :update, :id => other.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
549 }.should raise_error(ActiveRecord::Forbidden)
553 context '検証、保存に失敗したとき' do
556 Comic.any_instance.stub(:update_attributes).and_return(false)
557 put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
558 response.should render_template("edit")
562 it 'ステータスコード422 unprocessable_entity を返す' do
563 Comic.any_instance.stub(:update_attributes).and_return(false)
564 put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
565 response.status.should eq 422
568 Comic.any_instance.stub(:update_attributes).and_return(false)
569 put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
571 JSON.parse(response.body)
\r
572 }.should_not raise_error(JSON::ParserError)
575 Comic.any_instance.stub(:update_attributes).and_return(false)
576 put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
577 json = JSON.parse response.body
578 json.should eq( {})
\r