X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=spec%2Fcontrollers%2Fpanels_controller_spec.rb;h=283feb17db1577074fe0fdde4cabbe7b13b715e0;hb=d06c85598de5091129d22bfdcc650fae261a6219;hp=2a2ae43caeb85b30ff5c676cc988b185c86f87c9;hpb=e238f6a0e79f30d8a9fca5f9af3a1fa6317f401a;p=pettanr%2Fpettanr.git diff --git a/spec/controllers/panels_controller_spec.rb b/spec/controllers/panels_controller_spec.rb index 2a2ae43c..283feb17 100644 --- a/spec/controllers/panels_controller_spec.rb +++ b/spec/controllers/panels_controller_spec.rb @@ -1,17 +1,19 @@ # -*- encoding: utf-8 -*- require 'spec_helper' - +#コマ describe PanelsController do before do Factory :admin - @license = Factory :license + @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 #ユーザ作成時に連動して作成される end describe '一覧表示に於いて' do before do - @panel = Factory :panel, :author_id => @user.author.id + @panel = Factory :panel, :author_id => @author.id Panel.stub(:list).and_return([@panel, @panel, @panel]) sign_in @user end @@ -73,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 @@ -137,7 +139,7 @@ describe PanelsController do it 'データがアレになっている' do get :show, :id => @panel.id, :format => :json json = JSON.parse response.body - json["width"].should match(/438/) + json["width"].should eq 100 end end end @@ -289,7 +291,8 @@ describe PanelsController do describe '新規作成に於いて' do before do - @panel = Factory :normal_panel, :author_id => @user.author.id + @panel = Factory :panel, :author_id => @user.author.id + @attr = Factory.attributes_for(:panel, :author_id => @author.id) sign_in @user end context '事前チェックする' do @@ -298,57 +301,65 @@ describe PanelsController do Panel.stub(:count).and_return(10) end it 'panelがパラメータにあれば、展開する' do - post :create, :panel => Factory.attributes_for(:panel, :panel_id => @panel.id) - assigns(:prm)['border'].to_i.should eq 1 + post :create, :panel => @attr + assigns(:prm)['border'].to_i.should eq @panel.border end it 'jsonがパラメータにあれば、展開する' do - post :create, :json => Factory.attributes_for(:panel, :panel_id => @panel.id, :border => 4).to_s + 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, :panel_id => @panel.id), - :json => Factory.attributes_for(:panel, :panel_id => @panel.id, :border => 4).to_s + :panel => Factory.attributes_for(:panel), + :json => Factory.attributes_for(:panel, :border => 4).to_json } - assigns(:prm)['border'].to_i.should eq 1 + 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(:save).exactly(1) - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id) + Panel.any_instance.should_receive(:store).exactly(1) + post :create, :panel => @attr end it "@panelに作成されたコマを保持していて、それがDBにある" do - post :create, :panel => Factory.attributes_for(:panel, :author => @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(:save).and_return(true) - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id) + Panel.any_instance.stub(:store).and_return(true) + post :create, :panel => @attr response.status.should eq 302 end it '作成されたコマの表示ページへ遷移する' do -# Panel.any_instance.stub(:save).and_return(true) - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id) +# 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(:save).and_return(true) - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json +# 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 => Factory.attributes_for(:panel, :author => @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 => @author.id), :format => :json + post :create, :panel => @attr, :format => :json json = JSON.parse response.body - json["title"].should match(/normal/) + json["width"].should eq @panel.width end end end @@ -358,50 +369,50 @@ describe PanelsController do end context 'html形式' do it 'ステータスコード302 Foundを返す' do - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id) + post :create, :panel => @attr response.status.should eq 302 end it 'サインインページへ遷移する' do - post :create, :panel => Factory.attributes_for(:panel, :author => @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 => @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 => @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 => @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 => @author.id) + post :create, :panel => @attr response.status.should eq 200 end it '新規ページを描画する' do - post :create, :panel => Factory.attributes_for(:panel, :author => @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 => @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 => @author.id), :format => :json + post :create, :panel => @attr, :format => :json response.message.should match(/Unprocessable/) end end @@ -469,100 +480,196 @@ describe PanelsController do describe '更新に於いて' do before do - @panel = Factory :panel, :author => @author.id + @panel = Factory :panel, :author_id => @user.author.id + @attr = Factory.attributes_for(:panel, :author_id => @author.id) sign_in @user end - context '事前チェックしておく' do - it 'コマモデルに単体取得を問い合わせている' do - Panel.stub(:show).with(any_args()).and_return @panel - Panel.should_receive(:show).exactly(1) - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) + context '事前チェックする' do + before do + controller + Panel.stub(:count).and_return(10) end - it 'モデルに更新を依頼する' do - Panel.any_instance.should_receive(:update_attributes).with(any_args) - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) + it 'panelがパラメータにあれば、展開する' do + put :update, :id => @panel.id, :panel => @attr + assigns(:prm)['border'].to_i.should eq @panel.border end - it '@panelにアレを取得している' do - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) - assigns(:panel).id.should eq(@panel.id) + 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 - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) - Panel.find(@panel.id).visible.should eq 0 + 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(:update_attributes).with(any_args()).and_return(true) - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) + Panel.any_instance.stub(:store).and_return(true) + put :update, :id => @panel.id, :panel => @attr response.status.should eq 302 end - it '更新されたコマの表示ページへ遷移する' do - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) - response.should redirect_to(@panel) + 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(:update_attributes).with(any_args()).and_return(true) - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json +# 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(:update_attributes).with(any_args()).and_return(true) - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json - response.body.should match /./ - end end end context '作家権限がないとき' do before do sign_out @user end - it 'ステータスコード302 Foundを返す' do - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) - response.status.should eq 302 - 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 => Factory.attributes_for(:panel, :author => @author.id) + 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 => Factory.attributes_for(:panel, :author => @author.id), :format => :json + put :update, :id => @panel.id, :panel => @attr, :format => :json response.message.should match(/Unauthorized/) end end end - context '検証、保存に失敗したとき' do + context '検証、保存に失敗した' do before do - Panel.any_instance.stub(:update_attributes).and_return(false) + 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 => Factory.attributes_for(:panel, :author => @author.id) + 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 => Factory.attributes_for(:panel, :author => @author.id) + put :update, :id => @panel.id, :panel => @attr response.should render_template("edit") end end context 'json形式' do it 'ステータスコード422 unprocessable_entity を返す' do - Panel.any_instance.stub(:update_attributes).and_return(false) - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json + 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 => Factory.attributes_for(:panel, :author => @author.id), :format => :json + 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 +