OSDN Git Service

t#30328:create op import ...and pull
[pettanr/pettanr.git] / spec / controllers / comics_controller_spec.rb
index 9efd0e0..c475426 100644 (file)
@@ -1,7 +1,6 @@
 # -*- encoding: utf-8 -*-\r
 require 'spec_helper'\r
 #コミック\r
-\r
 describe ComicsController do\r
   before do\r
     @admin =FactoryGirl.create :admin\r
@@ -12,6 +11,7 @@ describe ComicsController do
     @author = FactoryGirl.create :author, :user_id => @user.id\r
   end\r
   \r
+if MagicNumber['run_mode'] == 1\r
   describe '一覧表示に於いて' do\r
     before do\r
       @comic = FactoryGirl.create :comic, :author_id => @user.author.id\r
@@ -601,5 +601,500 @@ describe ComicsController do
     end\r
   end\r
 \r
+  describe '削除に於いて' do\r
+    before do\r
+      @comic = FactoryGirl.create :comic, :author => @author\r
+      sign_in @user\r
+    end\r
+    context '事前チェックしておく' do\r
+      before do\r
+        Comic.stub(:edit).with(any_args()).and_return @comic\r
+        Comic.any_instance.stub(:destroy_with_story).with(any_args()).and_return(true)\r
+      end\r
+      it 'コミックモデルに編集取得を問い合わせている' do\r
+        Comic.should_receive(:edit).exactly(1)\r
+        delete :destroy, :id => @comic.id\r
+      end\r
+      it 'モデルに削除を依頼する' do\r
+        Comic.any_instance.should_receive(:destroy_with_story).exactly(1)\r
+        delete :destroy, :id => @comic.id\r
+      end\r
+      it '@comicにアレを取得している' do\r
+        delete :destroy, :id => @comic.id\r
+        assigns(:comic).id.should eq(@comic.id)\r
+      end\r
+    end\r
+    context 'つつがなく終わるとき' do\r
+      it '削除される' do\r
+        lambda {\r
+          delete :destroy, :id => @comic.id\r
+        }.should change Comic, :count\r
+      end\r
+      context 'html形式' do\r
+        before do\r
+          Comic.any_instance.stub(:destroy_with_story).with(any_args()).and_return(true)\r
+        end\r
+        it 'ステータスコード302 Foundを返す' do\r
+          delete :destroy, :id => @comic.id\r
+          response.status.should eq 302\r
+        end\r
+        it 'マイコミックの一覧ページへ遷移する' do\r
+          delete :destroy, :id => @comic.id\r
+          response.should redirect_to('/home/comic')\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        before do\r
+          Comic.any_instance.stub(:destroy_with_story).with(any_args()).and_return(true)\r
+        end\r
+        it 'ステータスコード200 OKを返す' do\r
+          delete :destroy, :id => @comic.id, :format => :json\r
+          response.should be_success \r
+        end\r
+        it 'ページ本体は特に返さない' do\r
+          delete :destroy, :id => @comic.id, :format => :json\r
+          response.body.should match /./\r
+        end\r
+      end\r
+    end\r
+    context '作家権限がないとき' do\r
+      before do\r
+        sign_out @user\r
+      end\r
+      it 'ステータスコード302 Foundを返す' do\r
+        delete :destroy, :id => @comic.id\r
+        response.status.should eq 302\r
+      end\r
+      context 'html形式' do\r
+        it 'サインインページへ遷移する' do\r
+          delete :destroy, :id => @comic.id\r
+          response.body.should redirect_to '/users/sign_in'\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it '応答メッセージにUnauthorizedを返す' do\r
+          delete :destroy, :id => @comic.id, :format => :json\r
+          response.message.should match(/Unauthorized/)\r
+        end\r
+      end\r
+    end\r
+    context '削除に失敗したとき' do\r
+      before do\r
+        Comic.any_instance.stub(:destroy_with_story).and_return(false)\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\r
+          delete :destroy, :id => @comic.id\r
+          response.status.should eq 302\r
+        end\r
+        it 'そのコミックの詳細ページへ遷移する' do\r
+          delete :destroy, :id => @comic.id\r
+          response.should redirect_to(comic_path(@comic))\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'ステータスコード422 unprocessable_entity を返す' do\r
+          delete :destroy, :id => @comic.id, :format => :json\r
+          response.status.should eq 422\r
+        end\r
+        it '応答メッセージUnprocessable Entityを返す' do\r
+          delete :destroy, :id => @comic.id, :format => :json\r
+          response.message.should match(/Unprocessable/)\r
+        end\r
+      end\r
+    end\r
+  end\r
+\r
+else\r
+  describe '一覧表示に於いて' do\r
+    before do\r
+      @comic = FactoryGirl.create :comic, :author_id => @user.author.id\r
+      Comic.stub(:list).and_return([@comic, @comic, @comic])\r
+      sign_in @user\r
+    end\r
+    context 'つつがなく終わるとき' do\r
+      it 'ステータスコード200 OKを返す' do\r
+        get :index\r
+        response.should be_success \r
+      end\r
+      context 'html形式' do\r
+        it 'indexテンプレートを描画する' do\r
+          get :index\r
+          response.should render_template("index")\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'jsonデータを返す' do\r
+          get :index, :format => :json\r
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)\r
+        end\r
+      end\r
+    end\r
+    context '作家権限がないとき' do\r
+      before do\r
+        sign_out @user\r
+      end\r
+      it 'ステータスコード200 OKを返す' do\r
+        get :index\r
+        response.should be_success \r
+      end\r
+      context 'html形式' do\r
+        it 'indexテンプレートを描画する' do\r
+          get :index\r
+          response.should render_template("index")\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'jsonデータを返す' do\r
+          get :index, :format => :json\r
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)\r
+        end\r
+      end\r
+    end\r
+  end\r
+  \r
+  describe '単体表示に於いて' do\r
+    before do\r
+      @comic = FactoryGirl.create :comic, :author_id => @user.author.id, :title => 'normal'\r
+      Comic.stub(:show).and_return(@comic)\r
+      sign_in @user\r
+    end\r
+    context 'つつがなく終わるとき' do\r
+      it 'ステータスコード200 OKを返す' do\r
+        get :show, :id => @comic.id\r
+        response.should be_success\r
+      end\r
+      context 'html形式' do\r
+        it 'showテンプレートを描画する' do\r
+          get :show, :id => @comic.id\r
+          response.should render_template("show")\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'jsonデータを返す' do\r
+          get :show, :id => @comic.id, :format => :json\r
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)\r
+        end\r
+      end\r
+    end\r
+    context '作家権限がないとき' do\r
+      before do\r
+        sign_out @user\r
+      end\r
+      it 'ステータスコード200 OKを返す' do\r
+        get :show, :id => @comic.id\r
+        response.should be_success\r
+      end\r
+      context 'html形式' do\r
+        it 'showテンプレートを描画する' do\r
+          get :show, :id => @comic.id\r
+          response.should render_template("show")\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'jsonデータを返す' do\r
+          get :show, :id => @comic.id, :format => :json\r
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)\r
+        end\r
+      end\r
+    end\r
+  end\r
+  describe 'コミック数取得に於いて' do\r
+    before do\r
+      Comic.should_receive(:visible_count).and_return(3)\r
+      sign_out @user\r
+    end\r
+    context 'つつがなく終わるとき' do\r
+      it 'ステータスコード200 OKを返す' do\r
+        get :count, :format => :json\r
+        response.should be_success \r
+      end\r
+      context 'json形式' do\r
+        it 'jsonデータを返す' do\r
+          get :count, :format => :json\r
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)\r
+        end\r
+      end\r
+    end\r
+  end\r
+\r
+  describe '新規作成フォーム表示に於いて' do\r
+    before do\r
+      sign_in @user\r
+    end\r
+    context 'つつがなく終わるとき' do\r
+      it 'ステータスコード200 OKを返す' do\r
+        get :new\r
+        response.should be_success \r
+      end\r
+      context 'html形式' do\r
+        it 'newテンプレートを描画する' do\r
+          get :new\r
+          response.should render_template("new")\r
+        end\r
+      end\r
+      context 'js形式' do\r
+        it 'new.jsテンプレートを描画する' do\r
+          get :new, :format => :js\r
+          response.should render_template("new")\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'jsonデータを返す' do\r
+          get :new, :format => :json\r
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)\r
+        end\r
+      end\r
+    end\r
+    context '作家権限がないとき' do\r
+      before do\r
+        sign_out @user\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\r
+          get :new\r
+          response.status.should eq 302\r
+        end\r
+        it 'サインインページへ遷移する' do\r
+          get :new\r
+          response.body.should redirect_to '/users/sign_in'\r
+        end\r
+      end\r
+      context 'js形式' do\r
+        it 'ステータスコード401 Unauthorizedを返す' do\r
+          get :new, :format => :js\r
+          response.status.should eq 401\r
+        end\r
+        it '応答メッセージにUnauthorizedを返す' do\r
+          get :new, :format => :js\r
+          response.message.should match(/Unauthorized/)\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'ステータスコード401 Unauthorizedを返す' do\r
+          get :new, :format => :json\r
+          response.status.should eq 401\r
+        end\r
+        it '応答メッセージにUnauthorizedを返す' do\r
+          get :new, :format => :json\r
+          response.message.should match(/Unauthorized/)\r
+        end\r
+      end\r
+    end\r
+  end\r
+\r
+  describe '新規作成に於いて' do\r
+    before do\r
+      sign_in @user\r
+      @attr = FactoryGirl.attributes_for(:comic, :author_id => @author.id, :title => 'normal')\r
+    end\r
+    context 'つつがなく終わるとき' do\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\r
+          Comic.any_instance.stub(:save).and_return(true)\r
+          post :create, :comic => @attr\r
+          response.status.should eq 302\r
+        end\r
+        it '作成されたコミックの表示ページへ遷移する' do\r
+#          Comic.any_instance.stub(:save).and_return(true)\r
+          post :create, :comic => @attr\r
+          response.should redirect_to(Comic.last)\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'ステータスコード200 OKを返す' do\r
+#          Comic.any_instance.stub(:save).and_return(true)\r
+          post :create, :comic => @attr, :format => :json\r
+          response.should be_success \r
+        end\r
+        it '作成されたコミックをjsonデータで返す' do\r
+          post :create, :comic => @attr, :format => :json\r
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)\r
+        end\r
+      end\r
+    end\r
+    context '作家権限がないとき' do\r
+      before do\r
+        sign_out @user\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\r
+          post :create, :comic => @attr\r
+          response.status.should eq 302\r
+        end\r
+        it 'サインインページへ遷移する' do\r
+          post :create, :comic => @attr\r
+          response.body.should redirect_to '/users/sign_in'\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'ステータスコード401 Unauthorizedを返す' do\r
+          post :create, :comic => @attr, :format => :json\r
+          response.status.should eq 401\r
+        end\r
+        it '応答メッセージにUnauthorizedを返す' do\r
+          post :create, :comic => @attr, :format => :json\r
+          response.message.should match(/Unauthorized/)\r
+        end\r
+      end\r
+    end\r
+  end\r
+\r
+  describe '編集フォーム表示に於いて' do\r
+    before do\r
+      @comic = FactoryGirl.create :comic, :author_id => @user.author.id\r
+      sign_in @user\r
+      Comic.stub(:edit).and_return(@comic)\r
+    end\r
+    context 'つつがなく終わるとき' do\r
+      it 'ステータスコード200 OKを返す' do\r
+        get :edit, :id => @comic.id\r
+        response.should be_success \r
+      end\r
+      context 'html形式' do\r
+        it 'editテンプレートを描画する' do\r
+          get :edit, :id => @comic.id\r
+          response.should render_template("edit")\r
+        end\r
+      end\r
+      context 'js形式' do\r
+        it 'edit.jsテンプレートを描画する' do\r
+          get :edit, :id => @comic.id, :format => :js\r
+          response.should render_template("edit")\r
+        end\r
+      end\r
+    end\r
+    context '作家権限がないとき' do\r
+      before do\r
+        sign_out @user\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\r
+          get :edit, :id => @comic.id\r
+          response.status.should eq 302\r
+        end\r
+        it 'サインインページへ遷移する' do\r
+          get :edit, :id => @comic.id\r
+          response.body.should redirect_to '/users/sign_in'\r
+        end\r
+      end\r
+      context 'js形式' do\r
+        it 'ステータスコード401 Unauthorizedを返す' do\r
+          get :edit, :id => @comic.id, :format => :js\r
+          response.status.should eq 401\r
+        end\r
+        it '応答メッセージにUnauthorizedを返す' do\r
+          get :edit, :id => @comic.id, :format => :js\r
+          response.message.should match(/Unauthorized/)\r
+        end\r
+      end\r
+    end\r
+  end\r
+\r
+  describe '更新に於いて' do\r
+    before do\r
+      @comic = FactoryGirl.create :comic, :author => @author\r
+      @attr = FactoryGirl.attributes_for(:comic, :author_id => @author.id, :title => 'updated title', :visible => 0)\r
+      sign_in @user\r
+    end\r
+    context 'つつがなく終わるとき' do\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\r
+          Comic.any_instance.stub(:save).with(any_args()).and_return(true)\r
+          put :update, :id => @comic.id, :comic => @attr\r
+          response.status.should eq 302\r
+        end\r
+        it '更新されたコミックの表示ページへ遷移する' do\r
+          put :update, :id => @comic.id, :comic => @attr\r
+          response.should redirect_to(@comic)\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'ステータスコード200 OKを返す' do\r
+          Comic.any_instance.stub(:save).with(any_args()).and_return(true)\r
+          put :update, :id => @comic.id, :comic => @attr, :format => :json\r
+          response.should be_success \r
+        end\r
+        it 'ページ本体は特に返さない' do\r
+          Comic.any_instance.stub(:save).with(any_args()).and_return(true)\r
+          put :update, :id => @comic.id, :comic => @attr, :format => :json\r
+          response.body.should match /./\r
+        end\r
+      end\r
+    end\r
+    context '作家権限がないとき' do\r
+      before do\r
+        sign_out @user\r
+      end\r
+      it 'ステータスコード302 Foundを返す' do\r
+        put :update, :id => @comic.id, :comic => @attr\r
+        response.status.should eq 302\r
+      end\r
+      context 'html形式' do\r
+        it 'サインインページへ遷移する' do\r
+          put :update, :id => @comic.id, :comic => @attr\r
+          response.body.should redirect_to '/users/sign_in'\r
+        end\r
+      end\r
+    end\r
+  end\r
+\r
+  describe '削除に於いて' do\r
+    before do\r
+      @comic = FactoryGirl.create :comic, :author => @author\r
+      sign_in @user\r
+    end\r
+    context 'つつがなく終わるとき' do\r
+      context 'html形式' do\r
+        before do\r
+          Comic.any_instance.stub(:destroy_with_story).with(any_args()).and_return(true)\r
+        end\r
+        it 'ステータスコード302 Foundを返す' do\r
+          delete :destroy, :id => @comic.id\r
+          response.status.should eq 302\r
+        end\r
+        it 'マイコミックの一覧ページへ遷移する' do\r
+          delete :destroy, :id => @comic.id\r
+          response.should redirect_to('/home/comic')\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        before do\r
+          Comic.any_instance.stub(:destroy_with_story).with(any_args()).and_return(true)\r
+        end\r
+        it 'ステータスコード200 OKを返す' do\r
+          delete :destroy, :id => @comic.id, :format => :json\r
+          response.should be_success \r
+        end\r
+        it 'ページ本体は特に返さない' do\r
+          delete :destroy, :id => @comic.id, :format => :json\r
+          response.body.should match /./\r
+        end\r
+      end\r
+    end\r
+    context '作家権限がないとき' do\r
+      before do\r
+        sign_out @user\r
+      end\r
+      it 'ステータスコード302 Foundを返す' do\r
+        delete :destroy, :id => @comic.id\r
+        response.status.should eq 302\r
+      end\r
+      context 'html形式' do\r
+        it 'サインインページへ遷移する' do\r
+          delete :destroy, :id => @comic.id\r
+          response.body.should redirect_to '/users/sign_in'\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it '応答メッセージにUnauthorizedを返す' do\r
+          delete :destroy, :id => @comic.id, :format => :json\r
+          response.message.should match(/Unauthorized/)\r
+        end\r
+      end\r
+    end\r
+  end\r
+\r
+end\r
 \r
 end\r