OSDN Git Service

add: social button
[pettanr/pettanr.git] / spec / controllers / comics_controller_spec.rb
index 5d2f639..449cbcb 100644 (file)
@@ -1,16 +1,17 @@
 # -*- encoding: utf-8 -*-\r
 require 'spec_helper'\r
-\r
+#コミック\r
 describe ComicsController do\r
   before do\r
-    FactoryGirl.create :admin\r
+    @admin =FactoryGirl.create :admin\r
     @sp = FactoryGirl.create :system_picture\r
     @lg = FactoryGirl.create :license_group\r
     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id\r
     @user = FactoryGirl.create :user_yas\r
-    @author = @user.author    #ユーザ作成時に連動して作成される\r
+    @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
@@ -57,6 +58,10 @@ describe ComicsController do
         assigns(:comics).should have_at_least(3).items\r
       end\r
       context 'html形式' do\r
+        it '@paginateにページ制御を取得している' do\r
+          get :index\r
+          assigns(:paginate).is_a?(Kaminari::PaginatableArray).should be_true\r
+        end\r
         it 'indexテンプレートを描画する' do\r
           get :index\r
           response.should render_template("index")\r
@@ -67,6 +72,10 @@ describe ComicsController do
           get :index, :format => :json\r
           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)\r
         end\r
+        it 'コミックモデルにjson一覧出力オプションを問い合わせている' do\r
+          Comic.should_receive(:list_json_opt).exactly(1)\r
+          get :index, :format => :json\r
+        end\r
         it 'データがリスト構造になっている' do\r
           get :index, :format => :json\r
           json = JSON.parse response.body\r
@@ -76,10 +85,11 @@ describe ComicsController do
           get :index, :format => :json\r
           json = JSON.parse response.body\r
           json.first.has_key?("title").should be_true\r
+          json.first.has_key?("visible").should be_true\r
         end\r
       end\r
     end\r
-    context 'ä½\9c家権é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do\r
+    context 'ã\83¦ã\83¼ã\82¶æ¨©é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do\r
       before do\r
         sign_out @user\r
       end\r
@@ -104,6 +114,27 @@ describe ComicsController do
         end\r
       end\r
     end\r
+    context 'ユーザ権限はないが管理者権限があるとき' do\r
+      before do\r
+        sign_out @user\r
+        sign_in @admin\r
+      end\r
+      it 'ステータスコード200 OKを返す' do\r
+        get :index\r
+        response.should be_success \r
+      end\r
+    end\r
+    context 'ユーザだが作家登録していないとき' do\r
+      before do\r
+        @author.destroy\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード200 OKを返す' do\r
+          get :index\r
+          response.should be_success \r
+        end\r
+      end\r
+    end\r
   end\r
   \r
   describe '単体表示に於いて' do\r
@@ -136,14 +167,19 @@ describe ComicsController do
           get :show, :id => @comic.id, :format => :json\r
           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)\r
         end\r
+        it 'コミックモデルにjson単体出力オプションを問い合わせている' do\r
+          Comic.should_receive(:show_json_opt).exactly(1)\r
+          get :show, :id => @comic.id, :format => :json\r
+        end\r
         it 'データがアレになっている' do\r
           get :show, :id => @comic.id, :format => :json\r
           json = JSON.parse response.body\r
           json["title"].should match(/normal/)\r
+          json["visible"].should_not be_nil\r
         end\r
       end\r
     end\r
-    context 'ä½\9c家権é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do\r
+    context 'ã\83¦ã\83¼ã\82¶æ¨©é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do\r
       before do\r
         sign_out @user\r
       end\r
@@ -168,6 +204,27 @@ describe ComicsController do
         end\r
       end\r
     end\r
+    context 'ユーザ権限はないが管理者権限があるとき' do\r
+      before do\r
+        sign_out @user\r
+        sign_in @admin\r
+      end\r
+      it 'ステータスコード200 OKを返す' do\r
+        get :show, :id => @comic.id\r
+        response.should be_success \r
+      end\r
+    end\r
+    context 'ユーザだが作家登録していないとき' do\r
+      before do\r
+        @author.destroy\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード200 OKを返す' do\r
+          get :show, :id => @comic.id\r
+          response.should be_success\r
+        end\r
+      end\r
+    end\r
 =begin\r
     context '対象コミックがないとき' do\r
       context 'html形式' do\r
@@ -260,8 +317,18 @@ describe ComicsController do
           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
+        it 'コミックモデルにjson単体出力オプションを問い合わせている' do\r
+          Comic.should_receive(:show_json_opt).exactly(1)\r
+          get :new, :format => :json\r
+        end\r
+      end\r
     end\r
-    context 'ä½\9c家権é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do\r
+    context 'ã\83¦ã\83¼ã\82¶æ¨©é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do\r
       before do\r
         sign_out @user\r
       end\r
@@ -285,6 +352,47 @@ describe ComicsController do
           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
+    context 'ユーザ権限はないが管理者権限があるとき' do\r
+      before do\r
+        sign_out @user\r
+        sign_in @admin\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
+    end\r
+    context 'ユーザだが作家登録していないとき' do\r
+      before do\r
+        @author.destroy\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\r
+          get :new, @attr\r
+          response.status.should eq 302\r
+        end\r
+        it '作家登録ページへ遷移する' do\r
+          get :new, @attr\r
+          response.body.should redirect_to new_author_path\r
+        end\r
+      end\r
     end\r
   end\r
 \r
@@ -293,11 +401,25 @@ describe ComicsController do
       sign_in @user\r
       @attr = FactoryGirl.attributes_for(:comic, :author_id => @author.id, :title => 'normal')\r
     end\r
-    context 'つつがなく終わるとき' do\r
+    context '事前チェックしておく' do\r
+      it 'コミックモデルにデフォルト値補充を依頼している' do\r
+        Comic.any_instance.should_receive(:supply_default).exactly(1)\r
+        post :create, :comic => @attr\r
+      end\r
+      it 'コミックモデルにカラム値復元を依頼している' do\r
+        Comic.any_instance.should_receive(:attributes=).exactly(1)\r
+        post :create, :comic => @attr\r
+      end\r
+      it 'コミックモデルに上書き補充を依頼している' do\r
+        Comic.any_instance.should_receive(:overwrite).exactly(1)\r
+        post :create, :comic => @attr\r
+      end\r
       it 'モデルに保存依頼する' do\r
         Comic.any_instance.should_receive(:save).exactly(1)\r
         post :create, :comic => @attr\r
       end\r
+    end\r
+    context 'つつがなく終わるとき' do\r
       it "@comicに作成されたコミックを保持していて、それがDBにある" do\r
         post :create, :comic => @attr\r
         assigns(:comic).should be_a(Comic)\r
@@ -329,10 +451,11 @@ describe ComicsController do
           post :create, :comic => @attr, :format => :json\r
           json = JSON.parse response.body\r
           json["title"].should match(/normal/)\r
+          json["visible"].should_not be_nil\r
         end\r
       end\r
     end\r
-    context 'ä½\9c家権é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do\r
+    context 'ã\83¦ã\83¼ã\82¶æ¨©é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do\r
       before do\r
         sign_out @user\r
       end\r
@@ -357,31 +480,62 @@ describe ComicsController do
         end\r
       end\r
     end\r
+    context 'ユーザ権限はないが管理者権限があるとき' do\r
+      before do\r
+        sign_out @user\r
+        sign_in @admin\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
+    end\r
+    context 'ユーザだが作家登録していないとき' do\r
+      before do\r
+        @author.destroy\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 new_author_path\r
+        end\r
+      end\r
+    end\r
     context '検証、保存に失敗した' do\r
       before do\r
         Comic.any_instance.stub(:save).and_return(false)\r
       end\r
       it "未保存のコミックを保持している" do\r
-        post :create, :comic => {}\r
+        post :create, :comic => @attr\r
         assigns(:comic).should be_a_new(Comic)\r
       end\r
       context 'html形式' do\r
         it 'ステータスコード200 OKを返す' do\r
-          post :create, :comic => {}\r
+          post :create, :comic => @attr\r
           response.status.should eq 200\r
         end\r
         it '新規ページを描画する' do\r
-          post :create, :comic => {}\r
+          post :create, :comic => @attr\r
           response.should render_template("new")\r
         end\r
       end\r
       context 'json形式' do\r
         it 'ステータスコード422 unprocessable_entity を返す' do\r
-          post :create, :comic => {}, :format => :json\r
+          post :create, :comic => @attr, :format => :json\r
           response.status.should eq 422\r
         end\r
         it '応答メッセージUnprocessable Entityを返す' do\r
-          post :create, :comic => {}, :format => :json\r
+          post :create, :comic => @attr, :format => :json\r
           response.message.should match(/Unprocessable/)\r
         end\r
       end\r
@@ -420,7 +574,7 @@ describe ComicsController do
         end\r
       end\r
     end\r
-    context 'ä½\9c家権é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do\r
+    context 'ã\83¦ã\83¼ã\82¶æ¨©é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do\r
       before do\r
         sign_out @user\r
       end\r
@@ -445,6 +599,37 @@ describe ComicsController do
         end\r
       end\r
     end\r
+    context 'ユーザ権限はないが管理者権限があるとき' do\r
+      before do\r
+        sign_out @user\r
+        sign_in @admin\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
+    end\r
+    context 'ユーザだが作家登録していないとき' do\r
+      before do\r
+        @author.destroy\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 new_author_path\r
+        end\r
+      end\r
+    end\r
   end\r
 \r
   describe '更新に於いて' do\r
@@ -459,6 +644,14 @@ describe ComicsController do
         Comic.should_receive(:edit).exactly(1)\r
         put :update, :id => @comic.id, :comic => @attr\r
       end\r
+      it 'コミックモデルにカラム値復元を依頼している' do\r
+        Comic.any_instance.should_receive(:attributes=).exactly(1)\r
+        put :update, :id => @comic.id, :comic => @attr\r
+      end\r
+      it 'コミックモデルに上書き補充を依頼している' do\r
+        Comic.any_instance.should_receive(:overwrite).exactly(1)\r
+        put :update, :id => @comic.id, :comic => @attr\r
+      end\r
       it 'モデルに更新を依頼する' do\r
         Comic.any_instance.stub(:save).with(any_args).and_return true\r
         Comic.any_instance.should_receive(:save).exactly(1)\r
@@ -498,15 +691,15 @@ describe ComicsController do
         end\r
       end\r
     end\r
-    context 'ä½\9c家権é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do\r
+    context 'ã\83¦ã\83¼ã\82¶æ¨©é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' 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 'ステータスコード302 Foundを返す' do\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.body.should redirect_to '/users/sign_in'\r
@@ -519,6 +712,37 @@ describe ComicsController do
         end\r
       end\r
     end\r
+    context 'ユーザ権限はないが管理者権限があるとき' do\r
+      before do\r
+        sign_out @user\r
+        sign_in @admin\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\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.body.should redirect_to '/users/sign_in'\r
+        end\r
+      end\r
+    end\r
+    context 'ユーザだが作家登録していないとき' do\r
+      before do\r
+        @author.destroy\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\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.body.should redirect_to new_author_path\r
+        end\r
+      end\r
+    end\r
     context '検証、保存に失敗したとき' do\r
       before do\r
         Comic.any_instance.stub(:save).and_return(false)\r
@@ -547,5 +771,531 @@ 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/comics')\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
+      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.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
+        sign_out @user\r
+        sign_in @admin\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.body.should redirect_to '/users/sign_in'\r
+        end\r
+      end\r
+    end\r
+    context 'ユーザだが作家登録していないとき' do\r
+      before do\r
+        @author.destroy\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.body.should redirect_to new_author_path\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