X-Git-Url: http://git.osdn.net/view?p=pettanr%2Fpettanr.git;a=blobdiff_plain;f=spec%2Fcontrollers%2Fpanels_controller_spec.rb;h=283feb17db1577074fe0fdde4cabbe7b13b715e0;hp=2b3f17ff8c2e535beca692beae817bc1700f2c2b;hb=d06c85598de5091129d22bfdcc650fae261a6219;hpb=1a2fb1c39c1f373494a584eb3cb54adb7a733375 diff --git a/spec/controllers/panels_controller_spec.rb b/spec/controllers/panels_controller_spec.rb index 2b3f17ff..283feb17 100644 --- a/spec/controllers/panels_controller_spec.rb +++ b/spec/controllers/panels_controller_spec.rb @@ -1,6 +1,6 @@ # -*- encoding: utf-8 -*- require 'spec_helper' - +#コマ describe PanelsController do before do Factory :admin @@ -9,7 +9,6 @@ describe PanelsController do @license = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id @user = Factory :user_yas @author = @user.author #ユーザ作成時に連動して作成される - @comic = Factory :comic, :author_id => @author.id end describe '一覧表示に於いて' do @@ -76,7 +75,7 @@ describe PanelsController do it 'リストの先頭くらいはコマっぽいものであって欲しい' do get :index, :format => :json json = JSON.parse response.body - json.first.has_key?("comic_id").should be_true + json.first.has_key?("border").should be_true end end end @@ -109,7 +108,7 @@ describe PanelsController do describe '単体表示に於いて' do before do - @panel = Factory :panel, :author_id => @user.author.id, :comic_id => @comic.id + @panel = Factory :panel, :author_id => @user.author.id Panel.stub(:show).and_return(@panel) sign_in @user end @@ -292,7 +291,8 @@ describe PanelsController do describe '新規作成に於いて' do before do - @panel = Factory :panel, :author_id => @user.author.id, :comic_id => @comic.id + @panel = Factory :panel, :author_id => @user.author.id + @attr = Factory.attributes_for(:panel, :author_id => @author.id) sign_in @user end context '事前チェックする' do @@ -301,55 +301,63 @@ describe PanelsController do Panel.stub(:count).and_return(10) end it 'panelがパラメータにあれば、展開する' do - post :create, :panel => Factory.attributes_for(:panel, :comic_id => @comic.id) + post :create, :panel => @attr assigns(:prm)['border'].to_i.should eq @panel.border end it 'jsonがパラメータにあれば、展開する' do - post :create, :json => Factory.attributes_for(:panel, :comic_id => @comic.id, :border => 4).to_json + post :create, :json => Factory.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, :comic_id => @comic.id), - :json => Factory.attributes_for(:panel, :comic_id => @comic.id, :border => 4).to_json + :panel => Factory.attributes_for(:panel), + :json => Factory.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 => Factory.attributes_for(:panel, :author_id => @author.id) + post :create, :panel => @attr end it "@panelに作成されたコマを保持していて、それがDBにある" do - post :create, :panel => Factory.attributes_for(:panel, :author_id => @author.id) + post :create, :panel => @attr assigns(:panel).should be_a(Panel) assigns(:panel).should be_persisted end context 'html形式' do it 'ステータスコード302 Foundを返す' do Panel.any_instance.stub(:store).and_return(true) - post :create, :panel => Factory.attributes_for(:panel, :author_id => @author.id) + post :create, :panel => @attr response.status.should eq 302 end it '作成されたコマの表示ページへ遷移する' do # Panel.any_instance.stub(:store).and_return(true) - post :create, :panel => Factory.attributes_for(:panel, :author_id => @author.id) + 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 => Factory.attributes_for(:panel, :author_id => @author.id), :format => :json + post :create, :panel => @attr, :format => :json response.should be_success end it '作成されたコマをjsonデータで返す' do - post :create, :panel => Factory.attributes_for(:panel, :author_id => @author.id), :format => :json + post :create, :panel => @attr, :format => :json lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) end it 'データがアレになっている' do - post :create, :panel => Factory.attributes_for(:panel, :author_id => @author.id), :format => :json + post :create, :panel => @attr, :format => :json json = JSON.parse response.body json["width"].should eq @panel.width end @@ -361,50 +369,50 @@ describe PanelsController do end context 'html形式' do it 'ステータスコード302 Foundを返す' do - post :create, :panel => Factory.attributes_for(:panel, :author_id => @author.id) + post :create, :panel => @attr response.status.should eq 302 end it 'サインインページへ遷移する' do - post :create, :panel => Factory.attributes_for(:panel, :author_id => @author.id) + post :create, :panel => @attr response.body.should redirect_to '/users/sign_in' end end context 'json形式' do it 'ステータスコード401 Unauthorizedを返す' do - post :create, :panel => Factory.attributes_for(:panel, :author_id => @author.id), :format => :json + post :create, :panel => @attr, :format => :json response.status.should eq 401 end it '応答メッセージにUnauthorizedを返す' do - post :create, :panel => Factory.attributes_for(:panel, :author_id => @author.id), :format => :json + post :create, :panel => @attr, :format => :json response.message.should match(/Unauthorized/) end end end context '検証、保存に失敗した' do before do - Panel.any_instance.stub(:save).and_return(false) + Panel.any_instance.stub(:store).and_return(false) end it "未保存のコマを保持している" do - post :create, :panel => Factory.attributes_for(:panel, :author_id => @author.id) + post :create, :panel => @attr assigns(:panel).should be_a_new(Panel) end context 'html形式' do it 'ステータスコード200 OKを返す' do - post :create, :panel => Factory.attributes_for(:panel, :author_id => @author.id) + post :create, :panel => @attr response.status.should eq 200 end it '新規ページを描画する' do - post :create, :panel => Factory.attributes_for(:panel, :author_id => @author.id) + post :create, :panel => @attr response.should render_template("new") end end context 'json形式' do it 'ステータスコード422 unprocessable_entity を返す' do - post :create, :panel => Factory.attributes_for(:panel, :author_id => @author.id), :format => :json + post :create, :panel => @attr, :format => :json response.status.should eq 422 end it '応答メッセージUnprocessable Entityを返す' do - post :create, :panel => Factory.attributes_for(:panel, :author_id => @author.id), :format => :json + post :create, :panel => @attr, :format => :json response.message.should match(/Unprocessable/) end end @@ -413,7 +421,7 @@ describe PanelsController do describe '編集フォーム表示に於いて' do before do - @panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id + @panel = Factory :panel, :author_id => @author.id sign_in @user Panel.stub(:show).and_return(@panel) end @@ -470,4 +478,198 @@ describe PanelsController do end end + describe '更新に於いて' do + before do + @panel = Factory :panel, :author_id => @user.author.id + @attr = Factory.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 + put :update, :id => @panel.id, :panel => @attr + 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 + 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 + } + assigns(:prm)['border'].to_i.should eq @panel.border + end + end + context 'つつがなく終わるとき' do + it 'モデルに編集取得依頼する' do + Panel.stub(:edit).with(any_args).and_return(@panel) + 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 + end + it "@panelに作成されたコマを保持していて、それがDBにある" do + put :update, :id => @panel.id, :panel => @attr + assigns(:panel).should be_a(Panel) + assigns(:panel).should be_persisted + end + 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 + 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 + context '検証、保存に失敗した' do + before do + Panel.any_instance.stub(:store).and_return(false) + end + it "指定のコマを保持している" do + put :update, :id => @panel.id, :panel => @attr + assigns(:panel).should eq @panel + end + context 'html形式' do + it 'ステータスコード200 OKを返す' do + put :update, :id => @panel.id, :panel => @attr + response.status.should eq 200 + end + it '編集ページを描画する' do + put :update, :id => @panel.id, :panel => @attr + response.should render_template("edit") + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + put :update, :id => @panel.id, :panel => @attr, :format => :json + response.status.should eq 422 + end + it '応答メッセージUnprocessable Entityを返す' do + put :update, :id => @panel.id, :panel => @attr, :format => :json + response.message.should match(/Unprocessable/) + end + end + end + end + + describe '削除に於いて' do + before do + @panel = Factory :panel, :author_id => @user.author.id + Panel.stub(:edit).and_return(@panel) + sign_in @user + end + context 'つつがなく終わるとき' do + it 'コマモデルに編集取得を問い合わせている' do + Panel.should_receive(:edit).exactly(1) + delete :destroy, :id => @panel.id + end + it '@panelにアレを取得している' do + delete :destroy, :id => @panel.id + assigns(:panel).id.should eq(@panel.id) + end + it 'そのコマを一つのトランザクションで削除する' do + lambda { + delete :destroy, :id => @panel.id + }.should change(Panel, :count) + 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(panels_url) + 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 +=begin + context '対象コマがないとき' do + end + context '他人のコマだったとき' do + end +=end + end + end +