OSDN Git Service

fix test
authoryasushiito <yas@pen-chan.jp>
Mon, 30 Jun 2014 05:11:16 +0000 (14:11 +0900)
committeryasushiito <yas@pen-chan.jp>
Mon, 30 Jun 2014 05:11:16 +0000 (14:11 +0900)
app/assets/javascripts/work/controllers.js.coffee.erb
app/controllers/scrolls_controller.rb
spec/controllers/scrolls_controller_spec.rb
spec/support/controller_macros.rb

index 604a318..58a4dca 100644 (file)
@@ -26,6 +26,9 @@
       edit: {\r
         type: 'edit',\r
       },\r
       edit: {\r
         type: 'edit',\r
       },\r
+      destroy: {\r
+        type: 'destroy',\r
+      },\r
     },\r
   },\r
   scrolls: {\r
     },\r
   },\r
   scrolls: {\r
index 5c933c5..3b59b72 100644 (file)
@@ -96,7 +96,8 @@ class ScrollsController < ApplicationController
   end
   
   def destroy
   end
   
   def destroy
-    @item = Scroll.edit(params[:id], @operators)
+    set_model
+    @item = @my_model_class.edit(params[:id], @operators)
     respond_to do |format|
       if @item.destroy_with_leafs
         flash[:notice] = I18n.t('flash.notice.destroyed', :model => Scroll.model_name.human)
     respond_to do |format|
       if @item.destroy_with_leafs
         flash[:notice] = I18n.t('flash.notice.destroyed', :model => Scroll.model_name.human)
index cb047c0..bd76f30 100644 (file)
@@ -459,9 +459,9 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1
       end
       it '@itemにリクエストしたスクロールを取得している' do
         get :edit, id: @item_id
       end
       it '@itemにリクエストしたスクロールを取得している' do
         get :edit, id: @item_id
-        @item = assigns(:item)
-        expect(@item.is_a?(Scroll)).to be true
-        expect(@item.new_record?).not_to be true
+        expect(assigns(:item).is_a?(Scroll)).to be true
+        expect(assigns(:item).new_record?).not_to be true
+        expect(assigns(:item)).to eq(@item)
       end
       it '@formに入力フォーム(Bucket)を取得している' do
         get :edit, id: @item_id
       end
       it '@formに入力フォーム(Bucket)を取得している' do
         get :edit, id: @item_id
@@ -527,9 +527,9 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1
       it '@itemにリクエストしたアイテムを取得して, 更新している' do
         allow_save Scroll
         put_save @attr
       it '@itemにリクエストしたアイテムを取得して, 更新している' do
         allow_save Scroll
         put_save @attr
-        @item = assigns(:item)
-        expect(@item.is_a?(Scroll)).to be true
-        expect(@item.visible).to eq(0)
+        expect(assigns(:item).is_a?(Scroll)).to be true
+        expect(assigns(:item).visible).to eq(0)
+        expect(assigns(:item)).to eq(@item)
       end
       context 'html形式' do
         it 'ステータスコード302 Foundを返して, 作成されたアイテムの表示ページへ遷移する' do
       end
       context 'html形式' do
         it 'ステータスコード302 Foundを返して, 作成されたアイテムの表示ページへ遷移する' do
@@ -601,135 +601,93 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1
 
   describe '削除に於いて' do
     before do
 
   describe '削除に於いて' do
     before do
-      @scroll = FactoryGirl.create :scroll, :author => @author
-      sign_in @user
+      @item =  FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas
+      @item_id = @item.id
+      @attr = {:id => @item_id}
+      @attr_with_json = @attr.merge(:format => :json)
     end
     end
-    context '事前チェックしておく' do
+    context '正常系の共通処理' do
       before do
       before do
-        Scroll.stub(:edit).with(any_args()).and_return @scroll
-        Scroll.any_instance.stub(:destroy_with_scroll_panel).with(any_args()).and_return(true)
-      end
-      it 'スクロールモデルに編集取得を問い合わせている' do
-        Scroll.should_receive(:edit).exactly(1)
-        delete :destroy, :id => @scroll.id
+        sign_in @user_yas
       end
       end
-      it 'モデルに削除を依頼する' do
-        Scroll.any_instance.should_receive(:destroy_with_scroll_panel).exactly(1)
-        delete :destroy, :id => @scroll.id
+      it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do
+        allow_destroy Scroll
+        destroy_save @attr
+        expect(assigns(:my_model_class)).to be Scroll
       end
       end
-      it '@scrollにアレを取得している' do
-        delete :destroy, :id => @scroll.id
-        assigns(:scroll).id.should eq(@scroll.id)
+      it '@itemにリクエストしたアイテムを取得している' do
+        allow_destroy Scroll
+        destroy_save @attr
+        expect(assigns(:item).is_a?(Scroll)).to be true
+        expect(assigns(:item)).to eq(@item)
       end
       end
-    end
-    context '正常系の共通処理' do
-      it '削除される' do
+      it '削除されている' do
         lambda {
         lambda {
-          delete :destroy, :id => @scroll.id
+          destroy_save @attr
         }.should change Scroll, :count
       end
         }.should change Scroll, :count
       end
+      it 'ぶら下がるリーフも削除されている' do
+        lambda {
+          destroy_save @attr
+        }.should change ScrollPanel, :count
+      end
       context 'html形式' do
       context 'html形式' do
-        before do
-          Scroll.any_instance.stub(:destroy_with_scroll_panel).with(any_args()).and_return(true)
-        end
-        it 'ステータスコード302 Foundを返す' do
-          delete :destroy, :id => @scroll.id
-          response.status.should eq 302
-        end
-        it 'マイスクロールの一覧ページへ遷移する' do
-          delete :destroy, :id => @scroll.id
-          response.should redirect_to('/home/scrolls')
+        it 'ステータスコード302 Foundを返して, マイスクロールの一覧ページへ遷移する' do
+          destroy_save @attr
+          expect(response.status).to eq(302)
+          expect(response).to redirect_to '/home/scrolls'
         end
       end
       context 'json形式' do
         end
       end
       context 'json形式' do
-        before do
-          Scroll.any_instance.stub(:destroy_with_scroll_panel).with(any_args()).and_return(true)
-        end
-        it 'ステータスコード200 OKを返す' do
-          delete :destroy, :id => @scroll.id, :format => :json
-          response.should be_success 
-        end
-        it 'ページ本体は特に返さない' do
-          delete :destroy, :id => @scroll.id, :format => :json
-          response.body.should match /./
+        it 'ステータスコード200 OKを返して, ページ本体は特に返さない' do
+          destroy_save @attr_with_json
+          expect(response).to be_success
+          expect(response.message).to match /./
         end
       end
     end
         end
       end
     end
-    context 'ユーザ権限がないとき' do
+    context '検証、保存に失敗したとき' do
       before do
       before do
-        sign_out @user
+        sign_in @user_yas
+        reject_destroy Scroll
       end
       context 'html形式' do
       end
       context 'html形式' do
-        it 'ステータスコード302 Foundを返す' do
-          delete :destroy, :id => @scroll.id
-          response.status.should eq 302
-        end
-        it 'サインインページへ遷移する' do
-          delete :destroy, :id => @scroll.id
-          response.body.should redirect_to '/users/sign_in'
+        it 'ステータスコード302 Foundを返して, そのスクロールの詳細ページへ遷移する' do
+          destroy_save @attr
+          expect(response.status).to eq(302)
+          expect(response).to redirect_to @item
         end
       end
       context 'json形式' do
         end
       end
       context 'json形式' do
-        it '応答メッセージにUnauthorizedを返す' do
-          delete :destroy, :id => @scroll.id, :format => :json
-          response.message.should match(/Unauthorized/)
-        end
-      end
-    end
-    context 'ユーザ権限はないが管理者権限があるとき' do
-      before do
-        sign_out @user
-        sign_in @admin
-      end
-      context 'html形式' do
-        it 'ステータスコード302 Foundを返す' do
-          delete :destroy, :id => @scroll.id
-          response.status.should eq 302
-        end
-        it 'サインインページへ遷移する' do
-          delete :destroy, :id => @scroll.id
-          response.body.should redirect_to '/users/sign_in'
+        it 'ステータスコード422 unprocessable_entityを返して, 応答メッセージUnprocessable Entityを返す' do
+          destroy_save @attr_with_json
+          expect(response.status).to eq(422)
+          expect(response.message).to match(/Unprocessable/)
         end
       end
     end
         end
       end
     end
-    context 'ã\83¦ã\83¼ã\82¶ã\81 ã\81\8cä½\9c家ç\99»é\8c²ã\81\97ã\81¦ã\81\84ã\81ªã\81\84とき' do
+    context 'ã\82µã\82¤ã\83³ã\82¤ã\83³ã\81®ç\8a¶æ\85\8bã\81\8cä¾\8bå¤\96ç\9a\84ã\81ªとき' do
       before do
       before do
-        @author.destroy
+        allow_destroy Scroll
       end
       end
-      context 'html形式' do
-        it 'ステータスコード302 Foundを返す' do
-          delete :destroy, :id => @scroll.id
-          response.status.should eq 302
-        end
-        it '作家登録ページへ遷移する' do
-          delete :destroy, :id => @scroll.id
-          response.body.should redirect_to new_author_path
-        end
-      end
-    end
-    context '削除に失敗したとき' do
-      before do
-        Scroll.any_instance.stub(:destroy_with_scroll_panel).and_return(false)
+      it '創作活動してないROM専の読者には作家登録ページへ遷移する' do
+        sign_in @user_rom
+        destroy_save @attr
+        announce_regist_author_for_html
+        announce_regist_author_for_json :delete, @attr_with_json
       end
       end
-      context 'html形式' do
-        it 'ステータスコード302 Foundを返す' do
-          delete :destroy, :id => @scroll.id
-          response.status.should eq 302
-        end
-        it 'そのスクロールの詳細ページへ遷移する' do
-          delete :destroy, :id => @scroll.id
-          response.should redirect_to(scroll_path(@scroll))
-        end
+      it 'ゲスト(サインインしていない)はサインインページに遷移する' do
+        destroy_save @attr
+        announce_sign_in_for_html
+        destroy_save @attr_with_json
+        announce_sign_in_for_json
       end
       end
-      context 'json形式' do
-        it 'ステータスコード422 unprocessable_entity を返す' do
-          delete :destroy, :id => @scroll.id, :format => :json
-          response.status.should eq 422
-        end
-        it '応答メッセージUnprocessable Entityを返す' do
-          delete :destroy, :id => @scroll.id, :format => :json
-          response.message.should match(/Unprocessable/)
-        end
+      it 'ユーザではなく管理者でもサインインページに遷移する' do
+        sign_in @admin
+        destroy_save id: @item_id
+        announce_sign_in_for_html
+        destroy_save @attr_with_json
+        announce_sign_in_for_json
       end
     end
   end
       end
     end
   end
index ef7dd7c..7b8981a 100644 (file)
@@ -65,6 +65,10 @@ module ControllerMacros
     put action, params\r
   end\r
   \r
     put action, params\r
   end\r
   \r
+  def destroy_save params, action = :destroy\r
+    delete action, params\r
+  end\r
+  \r
   # 保存処理を成功したことにしてスキップする\r
   def allow_save my_model_class\r
     allow_any_instance_of(my_model_class).to receive(:save).and_return(true)\r
   # 保存処理を成功したことにしてスキップする\r
   def allow_save my_model_class\r
     allow_any_instance_of(my_model_class).to receive(:save).and_return(true)\r
@@ -75,6 +79,16 @@ module ControllerMacros
     allow_any_instance_of(my_model_class).to receive(:save).and_return(false)\r
   end\r
   \r
     allow_any_instance_of(my_model_class).to receive(:save).and_return(false)\r
   end\r
   \r
+  # 削除処理を成功したことにしてスキップする\r
+  def allow_destroy my_model_class\r
+    allow_any_instance_of(my_model_class).to receive(:destroy).and_return(true)\r
+  end\r
+  \r
+  # 削除処理を失敗したことにしてスキップする\r
+  def reject_destroy my_model_class\r
+    allow_any_instance_of(my_model_class).to receive(:destroy).and_return(false)\r
+  end\r
+  \r
   def show_action_return_profiler item_id\r
     # prof形式: ステータスコード200 OKを返す\r
     get :show, :id => item_id, :format => :prof\r
   def show_action_return_profiler item_id\r
     # prof形式: ステータスコード200 OKを返す\r
     get :show, :id => item_id, :format => :prof\r