1 # -*- encoding: utf-8 -*-
5 describe ArtistsController do
7 FactoryGirl.create :admin
8 @sp = FactoryGirl.create :system_picture
9 @lg = FactoryGirl.create :license_group
10 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11 @user = FactoryGirl.create( :user_yas)
12 @author = @user.author
13 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
16 describe '一覧表示に於いて' do
18 Artist.stub(:list).and_return([@artist, @artist, @artist])
22 it '与えられたpageがセットされている' do
23 get :index, :page => 5
24 assigns(:page).should eq 5
26 it '省略されると@pageに1値が入る' do
28 assigns(:page).should eq 1
30 it '与えられたpage_sizeがセットされている' do
31 get :index, :page_size => 15
32 assigns(:page_size).should eq 15
34 it '省略されると@page_sizeにデフォルト値が入る' do
36 assigns(:page_size).should eq Artist.default_page_size
38 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
39 get :index, :page_size => 1500
40 assigns(:page_size).should eq Artist.max_page_size
42 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
43 get :index, :page_size => 0
44 assigns(:page_size).should eq Artist.default_page_size
47 context 'つつがなく終わるとき' do
48 it 'ステータスコード200 OKを返す' do
50 response.should be_success
52 it '絵師モデルに一覧を問い合わせている' do
53 Artist.should_receive(:list).exactly(1)
56 it '@artistsにリストを取得している' do
58 assigns(:artists).should have_at_least(3).items
61 it 'indexテンプレートを描画する' do
63 response.should render_template("index")
68 get :index, :format => :json
69 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
71 it '絵師モデルにjson一覧出力オプションを問い合わせている' do
72 Artist.should_receive(:list_json_opt).exactly(1)
73 get :index, :format => :json
75 it 'データがリスト構造になっている' do
76 get :index, :format => :json
77 json = JSON.parse response.body
78 json.should have_at_least(3).items
80 it 'リストの先頭くらいは絵師っぽいものであって欲しい' do
81 get :index, :format => :json
82 json = JSON.parse response.body
83 json.first.has_key?("name").should be_true
87 context '作家権限がないとき' do
92 it 'ステータスコード302 Foundを返す' do
94 response.status.should eq 302
98 response.should redirect_to '/users/sign_in'
102 it 'ステータスコード401 Unauthorizedを返す' do
103 get :index, :format => :json
104 response.status.should eq 401
106 it '応答メッセージにUnauthorizedを返す' do
107 get :index, :format => :json
108 response.message.should match(/Unauthorized/)
116 Artist.stub(:show).and_return(@artist)
119 context 'つつがなく終わるとき' do
120 it 'ステータスコード200 OKを返す' do
121 get :show, :id => @artist.id
122 response.should be_success
124 it '絵師モデルに単体取得を問い合わせている' do
125 Artist.should_receive(:show).exactly(1)
128 it '@artistにアレを取得している' do
129 get :show, :id => @artist.id
130 assigns(:artist).should eq(@artist)
133 it 'showテンプレートを描画する' do
134 get :show, :id => @artist.id
135 response.should render_template("show")
140 get :show, :id => @artist.id, :format => :json
141 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
143 it '絵師モデルにjson単体出力オプションを問い合わせている' do
144 Artist.should_receive(:show_json_opt).exactly(1)
145 get :show, :id => @artist.id, :format => :json
148 get :show, :id => @artist.id, :format => :json
149 json = JSON.parse response.body
150 json["name"].should match(/yas/)
154 context '作家権限がないとき' do
159 it 'ステータスコード302 Foundを返す' do
160 get :show, :id => @artist.id
161 response.status.should eq 302
163 it 'サインインページへ遷移する' do
164 get :show, :id => @artist.id
165 response.body.should redirect_to '/users/sign_in'
169 it 'ステータスコード401 Unauthorizedを返す' do
170 get :show, :id => @artist.id, :format => :json
171 response.status.should eq 401
173 it '応答メッセージにUnauthorizedを返す' do
174 get :show, :id => @artist.id, :format => :json
175 response.message.should match(/Unauthorized/)
180 context '対象作家がないとき' do
182 it '例外404 not_foundを返す' do
185 }.should raise_error(ActiveRecord::RecordNotFound)
189 it '例外404 not_foundを返す' do
191 get :show, :id => 0, :format => :json
192 }.should raise_error(ActiveRecord::RecordNotFound)
199 describe '絵師数取得に於いて' do
201 Artist.should_receive(:visible_count).and_return(3)
204 context 'つつがなく終わるとき' do
205 it 'ステータスコード200 OKを返す' do
206 get :count, :format => :json
207 response.should be_success
211 get :count, :format => :json
212 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
214 it 'データがHash構造になっていて絵師数が3である' do
215 get :count, :format => :json
216 json = JSON.parse response.body
217 json["count"].should == 3
223 describe '新規作成フォーム表示に於いて' do
\r
227 context 'つつがなく終わるとき' do
\r
228 it 'ステータスコード200 OKを返す' do
\r
230 response.should be_success
\r
232 it '@artistに新規データを用意している' do
\r
234 assigns(:artist).should be_a_new(Artist)
\r
236 it '絵師モデルにデフォルト値補充を依頼している' do
\r
237 Artist.any_instance.should_receive(:supply_default).exactly(1)
\r
240 context 'html形式' do
\r
241 it 'newテンプレートを描画する' do
\r
243 response.should render_template("new")
\r
247 it 'new.jsテンプレートを描画する' do
\r
248 get :new, :format => :js
\r
249 response.should render_template("new")
\r
254 get :new, :format => :json
255 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
259 context '作家権限がないとき' do
\r
263 context 'html形式' do
\r
264 it 'ステータスコード302 Foundを返す' do
\r
266 response.status.should eq 302
\r
268 it 'サインインページへ遷移する' do
\r
270 response.body.should redirect_to '/users/sign_in'
\r
274 it 'ステータスコード401 Unauthorizedを返す' do
\r
275 get :new, :format => :js
\r
276 response.status.should eq 401
\r
278 it '応答メッセージにUnauthorizedを返す' do
\r
279 get :new, :format => :js
\r
280 response.message.should match(/Unauthorized/)
\r
284 it 'ステータスコード401 Unauthorizedを返す' do
285 get :new, :format => :json
286 response.status.should eq 401
288 it '応答メッセージにUnauthorizedを返す' do
289 get :new, :format => :json
290 response.message.should match(/Unauthorized/)
296 describe '新規作成に於いて' do
\r
299 @attr = FactoryGirl.attributes_for(:artist, :author_id => @author.id, :name => 'ken')
\r
301 context 'つつがなく終わるとき' do
\r
303 Artist.any_instance.should_receive(:save).exactly(1)
\r
304 post :create, :artist => @attr
\r
306 it "@artistに作成された絵師を保持していて、それがDBにある" do
\r
307 post :create, :artist => @attr
\r
308 assigns(:artist).should be_a(Artist)
\r
309 assigns(:artist).should be_persisted
\r
311 context 'html形式' do
\r
312 it 'ステータスコード302 Foundを返す' do
\r
313 Artist.any_instance.stub(:save).and_return(true)
\r
314 post :create, :artist => @attr
\r
315 response.status.should eq 302
\r
317 it '作成された絵師の表示ページへ遷移する' do
\r
318 # Artist.any_instance.stub(:save).and_return(true)
\r
319 post :create, :artist => @attr
\r
320 response.should redirect_to(Artist.last)
\r
323 context 'json形式' do
\r
324 it 'ステータスコード200 OKを返す' do
\r
325 # Artist.any_instance.stub(:save).and_return(true)
\r
326 post :create, :artist => @attr, :format => :json
\r
327 response.should be_success
\r
329 it '作成された絵師をjsonデータで返す' do
\r
330 post :create, :artist => @attr, :format => :json
\r
331 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
\r
333 it 'データがアレになっている' do
\r
334 post :create, :artist => @attr, :format => :json
\r
335 json = JSON.parse response.body
\r
336 json["name"].should match(/ken/)
\r
340 context '作家権限がないとき' do
\r
344 context 'html形式' do
\r
345 it 'ステータスコード302 Foundを返す' do
\r
346 post :create, :artist => @attr
\r
347 response.status.should eq 302
\r
349 it 'サインインページへ遷移する' do
\r
350 post :create, :artist => @attr
\r
351 response.body.should redirect_to '/users/sign_in'
\r
354 context 'json形式' do
\r
355 it 'ステータスコード401 Unauthorizedを返す' do
\r
356 post :create, :artist => @attr, :format => :json
\r
357 response.status.should eq 401
\r
359 it '応答メッセージにUnauthorizedを返す' do
\r
360 post :create, :artist => @attr, :format => :json
\r
361 response.message.should match(/Unauthorized/)
\r
365 context '検証、保存に失敗した' do
\r
367 Artist.any_instance.stub(:save).and_return(false)
\r
369 it "未保存の絵師を保持している" do
\r
370 post :create, :artist => @attr
\r
371 assigns(:artist).should be_a_new(Artist)
\r
373 context 'html形式' do
\r
374 it 'ステータスコード200 OKを返す' do
\r
375 post :create, :artist => @attr
\r
376 response.status.should eq 200
\r
379 post :create, :artist => @attr
\r
380 response.should render_template("new")
\r
383 context 'json形式' do
\r
384 it 'ステータスコード422 unprocessable_entity を返す' do
\r
385 post :create, :artist => @attr, :format => :json
\r
386 response.status.should eq 422
\r
388 it '応答メッセージUnprocessable Entityを返す' do
\r
389 post :create, :artist => @attr, :format => :json
\r
390 response.message.should match(/Unprocessable/)
\r
396 describe '編集フォーム表示に於いて' do
\r
399 Artist.stub(:edit).and_return(@artist)
\r
401 context 'つつがなく終わるとき' do
\r
402 it 'ステータスコード200 OKを返す' do
\r
403 get :edit, :id => @artist.id
\r
404 response.should be_success
\r
406 it '絵師モデルに編集取得を問い合わせている' do
\r
407 Artist.should_receive(:edit).exactly(1)
\r
408 get :edit, :id => @artist.id
\r
410 it '@artistにデータを用意している' do
\r
411 get :edit, :id => @artist.id
\r
412 assigns(:artist).should eq @artist
\r
414 context 'html形式' do
\r
415 it 'editテンプレートを描画する' do
\r
416 get :edit, :id => @artist.id
\r
417 response.should render_template("edit")
\r
421 it 'edit.jsテンプレートを描画する' do
\r
422 get :edit, :id => @artist.id, :format => :js
\r
423 response.should render_template("edit")
\r
427 context '作家権限がないとき' do
\r
431 context 'html形式' do
\r
432 it 'ステータスコード302 Foundを返す' do
\r
433 get :edit, :id => @artist.id
\r
434 response.status.should eq 302
\r
436 it 'サインインページへ遷移する' do
\r
437 get :edit, :id => @artist.id
\r
438 response.body.should redirect_to '/users/sign_in'
\r
442 it 'ステータスコード401 Unauthorizedを返す' do
\r
443 get :edit, :id => @artist.id, :format => :js
\r
444 response.status.should eq 401
\r
446 it '応答メッセージにUnauthorizedを返す' do
\r
447 get :edit, :id => @artist.id, :format => :js
\r
448 response.message.should match(/Unauthorized/)
\r
454 describe '更新に於いて' do
\r
456 @attr = FactoryGirl.attributes_for(:artist, :author_id => @author.id, :name => 'ryu')
\r
459 context '事前チェックしておく' do
\r
460 it '絵師モデルに編集取得を問い合わせている' do
\r
461 Artist.stub(:edit).with(any_args()).and_return @artist
\r
462 Artist.should_receive(:edit).exactly(1)
\r
463 put :update, :id => @artist.id, :artist => @attr
\r
465 it 'モデルに更新を依頼する' do
\r
466 Artist.any_instance.stub(:save).with(any_args).and_return true
\r
467 Artist.any_instance.should_receive(:save).exactly(1)
\r
468 put :update, :id => @artist.id, :artist => @attr
\r
470 it '@artistにアレを取得している' do
\r
471 put :update, :id => @artist.id, :artist => @attr
\r
472 assigns(:artist).should eq @artist
\r
475 context 'つつがなく終わるとき' do
\r
477 put :update, :id => @artist.id, :artist => @attr
\r
478 Artist.find(@artist.id).name.should eq 'ryu'
\r
480 context 'html形式' do
\r
481 it 'ステータスコード302 Foundを返す' do
\r
482 Artist.any_instance.stub(:save).with(any_args()).and_return(true)
\r
483 put :update, :id => @artist.id, :artist => @attr
\r
484 response.status.should eq 302
\r
486 it '更新された絵師の表示ページへ遷移する' do
\r
487 put :update, :id => @artist.id, :artist => @attr
\r
488 response.should redirect_to(@artist)
\r
491 context 'json形式' do
\r
492 it 'ステータスコード200 OKを返す' do
\r
493 Artist.any_instance.stub(:save).with(any_args()).and_return(true)
\r
494 put :update, :id => @artist.id, :artist => @attr, :format => :json
\r
495 response.should be_success
\r
497 it 'ページ本体は特に返さない' do
\r
498 Artist.any_instance.stub(:save).with(any_args()).and_return(true)
\r
499 put :update, :id => @artist.id, :artist => @attr, :format => :json
\r
500 response.body.should match /./
\r
504 context '作家権限がないとき' do
\r
508 it 'ステータスコード302 Foundを返す' do
\r
509 put :update, :id => @artist.id, :artist => @attr
\r
510 response.status.should eq 302
\r
512 context 'html形式' do
\r
513 it 'サインインページへ遷移する' do
\r
514 put :update, :id => @artist.id, :artist => @attr
\r
515 response.body.should redirect_to '/users/sign_in'
\r
518 context 'json形式' do
\r
519 it '応答メッセージにUnauthorizedを返す' do
\r
520 put :update, :id => @artist.id, :artist => @attr, :format => :json
\r
521 response.message.should match(/Unauthorized/)
\r
525 context '検証、保存に失敗したとき' do
\r
527 Artist.any_instance.stub(:save).and_return(false)
\r
529 context 'html形式' do
\r
530 it 'ステータスコード200 Okを返す' do
\r
531 put :update, :id => @artist.id, :artist => @attr
\r
532 response.status.should eq 200
\r
535 put :update, :id => @artist.id, :artist => @attr
\r
536 response.should render_template("edit")
\r
539 context 'json形式' do
\r
540 it 'ステータスコード422 unprocessable_entity を返す' do
\r
541 Artist.any_instance.stub(:save).and_return(false)
\r
542 put :update, :id => @artist.id, :artist => @attr, :format => :json
\r
543 response.status.should eq 422
\r
545 it '応答メッセージUnprocessable Entityを返す' do
\r
546 put :update, :id => @artist.id, :artist => @attr, :format => :json
\r
547 response.message.should match(/Unprocessable/)
\r