X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=spec%2Fcontrollers%2Fpanels_controller_spec.rb;h=3ccd6c63796118dc6dd49515236ac63b6aecee8a;hb=e78c0e2df127384f29383a25c9495d1c5e52df5b;hp=283feb17db1577074fe0fdde4cabbe7b13b715e0;hpb=d06c85598de5091129d22bfdcc650fae261a6219;p=pettanr%2Fpettanr.git diff --git a/spec/controllers/panels_controller_spec.rb b/spec/controllers/panels_controller_spec.rb index 283feb17..3ccd6c63 100644 --- a/spec/controllers/panels_controller_spec.rb +++ b/spec/controllers/panels_controller_spec.rb @@ -3,17 +3,18 @@ require 'spec_helper' #コマ describe PanelsController do before do - Factory :admin - @sp = Factory :system_picture - @lg = Factory :license_group - @license = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id - @user = Factory :user_yas - @author = @user.author #ユーザ作成時に連動して作成される + @admin = FactoryGirl.create :admin + @sp = FactoryGirl.create :system_picture + @lg = FactoryGirl.create :license_group + @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id + @user = FactoryGirl.create :user_yas + @author = FactoryGirl.create :author, :user_id => @user.id end +if MagicNumber['run_mode'] == 1 describe '一覧表示に於いて' do before do - @panel = Factory :panel, :author_id => @author.id + @panel = FactoryGirl.create :panel, :author_id => @author.id Panel.stub(:list).and_return([@panel, @panel, @panel]) sign_in @user end @@ -44,10 +45,6 @@ describe PanelsController do end end context 'つつがなく終わるとき' do - it 'ステータスコード200 OKを返す' do - get :index - response.should be_success - end it 'コマモデルに一覧を問い合わせている' do Panel.should_receive(:list).exactly(1) get :index @@ -57,16 +54,28 @@ describe PanelsController do assigns(:panels).should have_at_least(3).items end context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :index + response.should be_success + end it 'indexテンプレートを描画する' do get :index response.should render_template("index") end end context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :index, :format => :json + response.should be_success + end it 'jsonデータを返す' do get :index, :format => :json lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) end + it 'コマモデルにコマリストのjson出力を問い合わせている' do + Panel.should_receive(:list_as_json_text).exactly(1) + get :index, :format => :json + end it 'データがリスト構造になっている' do get :index, :format => :json json = JSON.parse response.body @@ -108,15 +117,11 @@ describe PanelsController do describe '単体表示に於いて' do before do - @panel = Factory :panel, :author_id => @user.author.id - Panel.stub(:show).and_return(@panel) + @panel = FactoryGirl.create :panel, :author_id => @user.author.id + Panel.stub(:show).with(@panel.id.to_s, @author).and_return(@panel) sign_in @user end context 'つつがなく終わるとき' do - it 'ステータスコード200 OKを返す' do - get :show, :id => @panel.id - response.should be_success - end it 'コマモデルに単体取得を問い合わせている' do Panel.should_receive(:show).exactly(1) get :show @@ -126,16 +131,28 @@ describe PanelsController do assigns(:panel).id.should eq(@panel.id) end context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @panel.id + response.should be_success + end it 'showテンプレートを描画する' do get :show, :id => @panel.id response.should render_template("show") end end context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @panel.id, :format => :json + response.should be_success + end it 'jsonデータを返す' do get :show, :id => @panel.id, :format => :json lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) end + it 'コマモデルにコマのjson出力を問い合わせている' do + Panel.any_instance.should_receive(:panel_elements_as_json).exactly(1) + get :show, :id => @panel.id, :format => :json + end it 'データがアレになっている' do get :show, :id => @panel.id, :format => :json json = JSON.parse response.body @@ -189,7 +206,7 @@ describe PanelsController do context 'html形式' do it '例外403 forbiddenを返す' do Panel.any_instance.stub(:visible?).with(any_args()).and_return(false) - hidden = Factory :hidden_panel, :author_id => @author.id + hidden = FactoryGirl.create :hidden_panel, :author_id => @author.id lambda{ get :show, :id => hidden }.should raise_error(ActiveRecord::Forbidden) @@ -198,7 +215,7 @@ describe PanelsController do context 'json形式' do it '例外403 forbiddenを返す' do Panel.any_instance.stub(:visible?).with(any_args()).and_return(false) - hidden = Factory :hidden_panel, :author_id => @author.id + hidden = FactoryGirl.create :hidden_panel, :author_id => @author.id lambda{ get :show, :id => hidden, :format => :json }.should raise_error(ActiveRecord::Forbidden) @@ -237,10 +254,6 @@ describe PanelsController do sign_in @user end context 'つつがなく終わるとき' do - it 'ステータスコード200 OKを返す' do - get :new - response.should be_success - end it '@panelに新規データを用意している' do get :new assigns(:panel).should be_a_new(Panel) @@ -250,6 +263,10 @@ describe PanelsController do get :new end context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :new + response.should be_success + end it 'newテンプレートを描画する' do get :new response.should render_template("new") @@ -261,6 +278,16 @@ describe PanelsController do response.should render_template("new") end end + context 'json形式' do + it 'jsonデータを返す' do + get :new, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'コマモデルにコマのjson出力を問い合わせている' do + Panel.any_instance.should_receive(:panel_elements_as_json).exactly(1) + get :new, :format => :json + end + end end context '作家権限がないとき' do before do @@ -291,13 +318,12 @@ describe PanelsController do describe '新規作成に於いて' do before do - @panel = Factory :panel, :author_id => @user.author.id - @attr = Factory.attributes_for(:panel, :author_id => @author.id) + @panel = FactoryGirl.build :panel, :author_id => @author.id + @attr = FactoryGirl.attributes_for(:panel, :author_id => @author.id) sign_in @user end context '事前チェックする' do before do - controller Panel.stub(:count).and_return(10) end it 'panelがパラメータにあれば、展開する' do @@ -305,30 +331,26 @@ describe PanelsController do assigns(:prm)['border'].to_i.should eq @panel.border end it 'jsonがパラメータにあれば、展開する' do - post :create, :json => Factory.attributes_for(:panel, :border => 4).to_json + post :create, :json => FactoryGirl.attributes_for(:panel, :border => 4).to_json assigns(:prm)['border'].to_i.should eq 4 end it 'panel・json両パラメータがあれば、panelを優先して展開する' do post :create, { - :panel => Factory.attributes_for(:panel), - :json => Factory.attributes_for(:panel, :border => 4).to_json + :panel => FactoryGirl.attributes_for(:panel), + :json => FactoryGirl.attributes_for(:panel, :border => 4).to_json } assigns(:prm)['border'].to_i.should eq @panel.border end - end - context 'つつがなく終わるとき' do it 'コマモデルにデフォルト値補充を依頼している' do Panel.any_instance.should_receive(:supply_default).exactly(1) post :create, :panel => @attr end - it 'コマモデルに上書き補充を依頼している' do - Panel.any_instance.should_receive(:overwrite).exactly(1) - post :create, :panel => @attr - end it 'モデルに保存依頼する' do Panel.any_instance.should_receive(:store).exactly(1) post :create, :panel => @attr end + end + context 'つつがなく終わるとき' do it "@panelに作成されたコマを保持していて、それがDBにある" do post :create, :panel => @attr assigns(:panel).should be_a(Panel) @@ -356,6 +378,10 @@ describe PanelsController do post :create, :panel => @attr, :format => :json lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) end + it 'コマモデルにコマのjson出力を問い合わせている' do + Panel.any_instance.should_receive(:panel_elements_as_json).exactly(1) + post :create, :panel => @attr, :format => :json + end it 'データがアレになっている' do post :create, :panel => @attr, :format => :json json = JSON.parse response.body @@ -421,17 +447,13 @@ describe PanelsController do describe '編集フォーム表示に於いて' do before do - @panel = Factory :panel, :author_id => @author.id + @panel = FactoryGirl.create :panel, :author_id => @author.id sign_in @user Panel.stub(:show).and_return(@panel) end context 'つつがなく終わるとき' do - it 'ステータスコード200 OKを返す' do - get :edit, :id => @panel.id - response.should be_success - end - it 'コマモデルに単体取得を問い合わせている' do - Panel.should_receive(:show).exactly(1) + it 'コマモデルに編集取得を問い合わせている' do + Panel.should_receive(:edit).exactly(1) get :edit, :id => @panel.id end it '@panelにデータを用意している' do @@ -439,12 +461,20 @@ describe PanelsController do assigns(:panel).should eq @panel end context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :edit, :id => @panel.id + response.should be_success + end it 'editテンプレートを描画する' do get :edit, :id => @panel.id response.should render_template("edit") end end context 'js形式' do + it 'ステータスコード200 OKを返す' do + get :edit, :id => @panel.id, :format => :js + response.should be_success + end it 'edit.jsテンプレートを描画する' do get :edit, :id => @panel.id, :format => :js response.should render_template("edit") @@ -480,8 +510,8 @@ describe PanelsController do describe '更新に於いて' do before do - @panel = Factory :panel, :author_id => @user.author.id - @attr = Factory.attributes_for(:panel, :author_id => @author.id) + @panel = FactoryGirl.create :panel, :author_id => @user.author.id + @attr = FactoryGirl.attributes_for(:panel, :author_id => @author.id) sign_in @user end context '事前チェックする' do @@ -494,14 +524,14 @@ describe PanelsController do assigns(:prm)['border'].to_i.should eq @panel.border end it 'jsonがパラメータにあれば、展開する' do - put :update, :id => @panel.id, :json => Factory.attributes_for(:panel, :border => 4).to_json + put :update, :id => @panel.id, :json => FactoryGirl.attributes_for(:panel, :border => 4).to_json assigns(:prm)['border'].to_i.should eq 4 end it 'panel・json両パラメータがあれば、panelを優先して展開する' do put :update, { :id => @panel.id, - :panel => Factory.attributes_for(:panel), - :json => Factory.attributes_for(:panel, :border => 4).to_json + :panel => FactoryGirl.attributes_for(:panel), + :json => FactoryGirl.attributes_for(:panel, :border => 4).to_json } assigns(:prm)['border'].to_i.should eq @panel.border end @@ -512,10 +542,6 @@ describe PanelsController do Panel.should_receive(:edit).exactly(1) put :update, :id => @panel.id, :panel => @attr end - it 'コマモデルに上書き補充を依頼している' do - Panel.any_instance.should_receive(:overwrite).exactly(1) - put :update, :id => @panel.id, :panel => @attr - end it 'モデルに保存依頼する' do Panel.any_instance.should_receive(:store).exactly(1) put :update, :id => @panel.id, :panel => @attr @@ -543,6 +569,11 @@ describe PanelsController do put :update, :id => @panel.id, :panel => @attr, :format => :json response.should be_success end + it 'ページ本体は特に返さない' do + Panel.any_instance.stub(:save).with(any_args()).and_return(true) + put :update, :id => @panel.id, :panel => @attr, :format => :json + response.body.should match /./ + end end end context '作家権限がないとき' do @@ -603,7 +634,7 @@ describe PanelsController do describe '削除に於いて' do before do - @panel = Factory :panel, :author_id => @user.author.id + @panel = FactoryGirl.create :panel, :author_id => @user.author.id Panel.stub(:edit).and_return(@panel) sign_in @user end @@ -616,7 +647,7 @@ describe PanelsController do delete :destroy, :id => @panel.id assigns(:panel).id.should eq(@panel.id) end - it 'そのコマを一つのトランザクションで削除する' do + it 'そのコマを削除する' do lambda { delete :destroy, :id => @panel.id }.should change(Panel, :count) @@ -626,9 +657,9 @@ describe PanelsController do delete :destroy, :id => @panel.id response.status.should eq 302 end - it 'コマ一覧ページへ遷移する' do + it 'マイコマ一覧ページへ遷移する' do delete :destroy, :id => @panel.id - response.should redirect_to(panels_url) + response.should redirect_to('/home/panel') end end context 'json形式' do @@ -663,6 +694,31 @@ describe PanelsController do end end end + context '削除に失敗したとき' do + before do + Panel.any_instance.stub(:destroy_with_elements).and_return(false) + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @panel.id + response.status.should eq 302 + end + it 'そのコマの詳細ページへ遷移する' do + delete :destroy, :id => @panel.id + response.should redirect_to(panel_path(@panel)) + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + delete :destroy, :id => @panel.id, :format => :json + response.status.should eq 422 + end + it '応答メッセージUnprocessable Entityを返す' do + delete :destroy, :id => @panel.id, :format => :json + response.message.should match(/Unprocessable/) + end + end + end =begin context '対象コマがないとき' do end @@ -671,5 +727,415 @@ describe PanelsController do =end end +else + describe '一覧表示に於いて' do + before do + @panel = FactoryGirl.create :panel, :author_id => @author.id + Panel.stub(:list).and_return([@panel, @panel, @panel]) + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :index + response.should be_success + end + it 'indexテンプレートを描画する' do + get :index + response.should render_template("index") + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :index, :format => :json + response.should be_success + end + it 'jsonデータを返す' do + get :index, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :index + response.should be_success + end + it 'indexテンプレートを描画する' do + get :index + response.should render_template("index") + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :index, :format => :json + response.should be_success + end + it 'jsonデータを返す' do + get :index, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + end + end + end + + describe '単体表示に於いて' do + before do + @panel = FactoryGirl.create :panel, :author_id => @user.author.id + Panel.stub(:show).with(@panel.id.to_s, @author).and_return(@panel) + Panel.stub(:show).with(@panel.id.to_s, nil).and_return(@panel) + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @panel.id + response.should be_success + end + it 'showテンプレートを描画する' do + get :show, :id => @panel.id + response.should render_template("show") + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @panel.id, :format => :json + response.should be_success + end + it 'jsonデータを返す' do + get :show, :id => @panel.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @panel.id + response.should be_success + end + it 'showテンプレートを描画する' do + get :show, :id => @panel.id + response.should render_template("show") + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @panel.id, :format => :json + response.should be_success + end + it 'jsonデータを返す' do + get :show, :id => @panel.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + end + end + end + + describe 'コマ数取得に於いて' do + before do + Panel.should_receive(:visible_count).and_return(3) +# sign_in @user + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :count, :format => :json + response.should be_success + end + context 'json形式' do + it 'jsonデータを返す' do + get :count, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + end + end + end + + describe '新規作成フォーム表示に於いて' do + before do + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :new + response.should be_success + end + it 'newテンプレートを描画する' do + get :new + response.should render_template("new") + end + end + context 'js形式' do + it 'new.jsテンプレートを描画する' do + get :new, :format => :js + response.should render_template("new") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :new, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :new + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :new + response.body.should redirect_to '/users/sign_in' + end + end + context 'js形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :new, :format => :js + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :new, :format => :js + response.message.should match(/Unauthorized/) + end + end + end + end + + describe '新規作成に於いて' do + before do + @panel = FactoryGirl.build :panel, :author_id => @author.id + @attr = FactoryGirl.attributes_for(:panel, :author_id => @author.id) + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + Panel.any_instance.stub(:store).and_return(true) + post :create, :panel => @attr + response.status.should eq 302 + end + it '作成されたコマの表示ページへ遷移する' do +# Panel.any_instance.stub(:store).and_return(true) + post :create, :panel => @attr + response.should redirect_to(Panel.last) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do +# Panel.any_instance.stub(:store).and_return(true) + post :create, :panel => @attr, :format => :json + response.should be_success + end + it '作成されたコマをjsonデータで返す' do + post :create, :panel => @attr, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + post :create, :panel => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + post :create, :panel => @attr + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + post :create, :panel => @attr, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + post :create, :panel => @attr, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + end + + describe '編集フォーム表示に於いて' do + before do + @panel = FactoryGirl.create :panel, :author_id => @author.id + sign_in @user + Panel.stub(:show).and_return(@panel) + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :edit, :id => @panel.id + response.should be_success + end + it 'editテンプレートを描画する' do + get :edit, :id => @panel.id + response.should render_template("edit") + end + end + context 'js形式' do + it 'ステータスコード200 OKを返す' do + get :edit, :id => @panel.id, :format => :js + response.should be_success + end + it 'edit.jsテンプレートを描画する' do + get :edit, :id => @panel.id, :format => :js + response.should render_template("edit") + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :edit, :id => @panel.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :edit, :id => @panel.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'js形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :edit, :id => @panel.id, :format => :js + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :edit, :id => @panel.id, :format => :js + response.message.should match(/Unauthorized/) + end + end + end + end + + describe '更新に於いて' do + before do + @panel = FactoryGirl.create :panel, :author_id => @user.author.id + @attr = FactoryGirl.attributes_for(:panel, :author_id => @author.id) + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + Panel.any_instance.stub(:store).and_return(true) + put :update, :id => @panel.id, :panel => @attr + response.status.should eq 302 + end + it '作成されたコマの表示ページへ遷移する' do +# Panel.any_instance.stub(:store).and_return(true) + put :update, :id => @panel.id, :panel => @attr + response.should redirect_to(Panel.last) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do +# Panel.any_instance.stub(:store).and_return(true) + put :update, :id => @panel.id, :panel => @attr, :format => :json + response.should be_success + end + it 'ページ本体は特に返さない' do + Panel.any_instance.stub(:save).with(any_args()).and_return(true) + put :update, :id => @panel.id, :panel => @attr, :format => :json + response.body.should match /./ + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + put :update, :id => @panel.id, :panel => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + put :update, :id => @panel.id, :panel => @attr + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + put :update, :id => @panel.id, :panel => @attr, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + put :update, :id => @panel.id, :panel => @attr, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + end + + describe '削除に於いて' do + before do + @panel = FactoryGirl.create :panel, :author_id => @user.author.id + Panel.stub(:edit).and_return(@panel) + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @panel.id + response.status.should eq 302 + end + it 'マイコマ一覧ページへ遷移する' do + delete :destroy, :id => @panel.id + response.should redirect_to('/home/panel') + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + delete :destroy, :id => @panel.id, :format => :json + response.should be_success + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @panel.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + delete :destroy, :id => @panel.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + delete :destroy, :id => @panel.id, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + delete :destroy, :id => @panel.id, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + end + +end end