OSDN Git Service

add story
[pettanr/pettanr.git] / spec / controllers / stories_controller_spec.rb
index 23611f1..33e15b0 100644 (file)
 require 'spec_helper'
 #ストーリー
 describe StoriesController do
+  before do
+    Factory :admin
+    @license = Factory :license
+    @user = Factory :user_yas
+    @author = @user.author    #ユーザ作成時に連動して作成される
+    @comic = Factory :comic, :author_id => @user.author.id
+    @panel = Factory :panel, :author_id => @author.id
+  end
+  
 
   describe '閲覧に於いて' do
     before do
-      @comic = Factory :comic, :author_id => @user.author.id
+      @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
       Comic.stub(:show).and_return(@comic)
       sign_in @user
     end
     context '事前チェックする' do
+    end
+    context 'つつがなく終わるとき' do
+    end
+    context '作家権限がないとき' do
+    end
+  end
+
+  describe '新規作成フォーム表示に於いて' do
+    before do
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :new
+        response.should be_success 
+      end
+      it '@storyに新規データを用意している' do
+        get :new
+        assigns(:story).should be_a_new(Story)
+      end
+      it 'コマモデルにデフォルト値補充を依頼している' do
+        Story.any_instance.should_receive(:supply_default).exactly(1)
+        get :new
+      end
+      context 'html形式' do
+        it 'newテンプレートを描画する' do
+          get :new
+          response.should render_template("new")
+        end
+      end
+      context 'js形式' do
+        it 'new.jsテンプレートを描画する' do
+          get :new, :format => :js
+          response.should render_template("new")
+        end
+      end
+    end
+    context '作家権限がないとき' do
       before do
-        Panel.stub(:count).and_return(10)
+        sign_out @user
       end
-      it '与えられたoffsetがセットされている' do
-        get :play, :id => @comic.id, :offset => 7
-        assigns(:offset).should eq 7
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :new
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :new
+          response.body.should redirect_to '/users/sign_in'
+        end
       end
-      it '省略されると@offsetに0値が入る' do
-        get :play, :id => @comic.id
-        assigns(:offset).should eq 0
+      context 'js形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :new, :format => :js
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :new, :format => :js
+          response.message.should match(/Unauthorized/)
+        end
       end
-      it 'コマ数以上が与えられるとコマ数-1が入る' do
-        get :play, :id => @comic.id, :offset => 10
-        assigns(:offset).should eq 9
+    end
+  end
+  
+  describe '新規作成に於いて' do
+    before do
+      @attr = Factory.attributes_for(:story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id)
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'モデルに保存依頼する' do
+        Story.any_instance.should_receive(:store).exactly(1)
+        post :create, :story => @attr
       end
-      it '負の値が与えられると末尾(コマ数)からさかのぼった値が入る' do
-        get :play, :id => @comic.id, :offset => -3
-        assigns(:offset).should eq 7
+      it "@storyに作成されたコマを保持していて、それがDBにある" do
+        post :create, :story => @attr
+        assigns(:story).should be_a(Story)
+        assigns(:story).should be_persisted
       end
-      it 'コマ数以上の負の値が与えられると計算できないので0値が入る' do
-        get :play, :id => @comic.id, :offset => -13
-        assigns(:offset).should eq 0
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          Story.any_instance.stub(:store).and_return(true)
+          post :create, :story => @attr
+          response.status.should eq 302
+        end
+        it 'コミックのストーリー表示へ遷移する' do
+#          Story.any_instance.stub(:store).and_return(true)
+          post :create, :story => @attr
+          response.should redirect_to(:action => :show, :id => @attr[:comic_id])
+        end
       end
-      it '与えられたcountがセットされている' do
-        get :play, :id => @comic.id, :count => 18
-        assigns(:panel_count).should eq 18
+      context 'js形式' do
+        it 'ステータスコード302 Foundを返す' do
+          Story.any_instance.stub(:store).and_return(true)
+          post :create, :story => @attr, :format => :js
+          response.status.should eq 302
+        end
+        it 'コミックのストーリー表示へ遷移する' do
+#          Story.any_instance.stub(:store).and_return(true)
+          post :create, :story => @attr, :format => :js
+          response.should redirect_to(:action => :show, :id => @attr[:comic_id])
+        end
       end
-      it '省略されると@countにデフォルト値が入る' do
-        get :play, :id => @comic.id
-        assigns(:panel_count).should eq Comic.default_panel_size
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+#          Story.any_instance.stub(:store).and_return(true)
+          post :create, :story => @attr, :format => :json
+          response.should be_success 
+        end
+        it '作成されたコマをjsonデータで返す' do
+          post :create, :story => @attr, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがアレになっている' do
+          post :create, :story => @attr, :format => :json
+          json = JSON.parse response.body
+          json["t"].should eq @story.t
+        end
       end
-      it '最大を超えると@countにデフォルト最大値が入る' do
-        get :play, :id => @comic.id, :count => 1500
-        assigns(:panel_count).should eq Comic.max_panel_size
+    end
+    context '作家権限がないとき' do
+      before do
+        sign_out @user
       end
-      it '不正な値が入ると@countにデフォルト最大値が入る' do
-        get :play, :id => @comic.id, :page_size => 0
-        assigns(:panel_count).should eq Comic.default_panel_size
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          post :create, :story => @attr
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          post :create, :story => @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          post :create, :story => @attr, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          post :create, :story => @attr, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
       end
     end
+    context '検証、保存に失敗した' do
+      before do
+        Story.any_instance.stub(:store).and_return(false)
+      end
+      it "未保存のコマを保持している" do
+        post :create, :story => @attr
+        assigns(:story).should be_a_new(Story)
+      end
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          post :create, :story => @attr
+          response.status.should eq 200
+        end
+        it '新規ページを描画する' do
+          post :create, :story => @attr
+          response.should render_template("new")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード422 unprocessable_entity を返す' do
+          post :create, :story => @attr, :format => :json
+          response.status.should eq 422
+        end
+        it '応答メッセージUnprocessable Entityを返す' do
+          post :create, :story => @attr, :format => :json
+          response.message.should match(/Unprocessable/)
+        end
+      end
+    end
+  end
+
+  describe '編集フォーム表示に於いて' do
+    before do
+      @story = Factory :story, :author_id => @author.id
+      sign_in @user
+      Story.stub(:show).and_return(@story)
+    end
     context 'つつがなく終わるとき' do
       it 'ステータスコード200 OKを返す' do
-        get :play, :id => @comic.id
-        response.should be_success
+        get :edit, :id => @story.id
+        response.should be_success 
       end
-      it 'ã\82³ã\83\9fã\83\83ã\82¯モデルに単体取得を問い合わせている' do
-        Comic.should_receive(:show).exactly(1)
-        get :play, :id => @comic.id
+      it 'ã\82³ã\83\9eモデルに単体取得を問い合わせている' do
+        Story.should_receive(:show).exactly(1)
+        get :edit, :id => @story.id
       end
-      it '@comicにアレを取得している' do
-        get :play, :id => @comic.id
-        assigns(:comic).id.should eq(@comic.id)
+      it '@storyにデータを用意している' do
+        get :edit, :id => @story.id
+        assigns(:story).should eq @story
       end
       context 'html形式' do
-        it 'playテンプレートを描画する' do
-          get :play, :id => @comic.id
-          response.should render_template("play")
+        it 'editテンプレートを描画する' do
+          get :edit, :id => @story.id
+          response.should render_template("edit")
         end
       end
-      context 'json形式' do
-        it 'jsonデータを返す' do
-          get :play, :id => @comic.id, :format => :json
-          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+      context 'js形式' do
+        it 'edit.jsテンプレートを描画する' do
+          get :edit, :id => @story.id, :format => :js
+          response.should render_template("edit")
         end
-        it 'データがアレになっている' do
-          get :play, :id => @comic.id, :format => :json
-          json = JSON.parse response.body
-          json["title"].should match(/normal/)
+      end
+    end
+    context '作家権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :edit, :id => @story.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :edit, :id => @story.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'js形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :edit, :id => @story.id, :format => :js
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :edit, :id => @story.id, :format => :js
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+  end
+
+  describe '更新に於いて' do
+    before do
+      @story = Factory :story, :author_id => @user.author.id
+      @attr = Factory.attributes_for(:story, :author_id => @author.id)
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'モデルに取得依頼する' do
+        Story.stub(:show).with(any_args).and_return(@story)
+        Story.should_receive(:show).exactly(1)
+        put :update, :id => @story.id, :story => @attr
+      end
+      it 'モデルに保存依頼する' do
+        Story.any_instance.should_receive(:store).exactly(1)
+        put :update, :id => @story.id, :story => @attr
+      end
+      it "@storyに作成されたストーリーを保持していて、それがDBにある" do
+        put :update, :id => @story.id, :story => @attr
+        assigns(:story).should be_a(Story)
+        assigns(:story).should be_persisted
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          Story.any_instance.stub(:store).and_return(true)
+          put :update, :id => @story.id, :story => @attr
+          response.status.should eq 302
+        end
+        it 'コミックのストーリー表示へ遷移する' do
+#          Story.any_instance.stub(:store).and_return(true)
+          put :update, :id => @story.id, :story => @attr
+          response.should redirect_to(:action => :show, :id => @attr[:comic_id])
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+#          Story.any_instance.stub(:store).and_return(true)
+          put :update, :id => @story.id, :story => @attr, :format => :json
+          response.should be_success 
         end
       end
     end
@@ -87,25 +301,54 @@ describe StoriesController do
       end
       context 'html形式' do
         it 'ステータスコード302 Foundを返す' do
-          get :play, :id => @comic.id
+          put :update, :id => @story.id, :story => @attr
           response.status.should eq 302
         end
         it 'サインインページへ遷移する' do
-          get :play, :id => @comic.id
+          put :update, :id => @story.id, :story => @attr
           response.body.should redirect_to '/users/sign_in'
         end
       end
       context 'json形式' do
         it 'ステータスコード401 Unauthorizedを返す' do
-          get :play, :id => @comic.id, :format => :json
+          put :update, :id => @story.id, :story => @attr, :format => :json
           response.status.should eq 401
         end
         it '応答メッセージにUnauthorizedを返す' do
-          get :play, :id => @comic.id, :format => :json
+          put :update, :id => @story.id, :story => @attr, :format => :json
           response.message.should match(/Unauthorized/)
         end
       end
     end
+    context '検証、保存に失敗した' do
+      before do
+        Story.any_instance.stub(:store).and_return(false)
+      end
+      it "指定のコマを保持している" do
+        put :update, :id => @story.id, :story => @attr
+        assigns(:story).should eq @story
+      end
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          put :update, :id => @story.id, :story => @attr
+          response.status.should eq 200
+        end
+        it '編集ページを描画する' do
+          put :update, :id => @story.id, :story => @attr
+          response.should render_template("edit")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード422 unprocessable_entity を返す' do
+          put :update, :id => @story.id, :story => @attr, :format => :json
+          response.status.should eq 422
+        end
+        it '応答メッセージUnprocessable Entityを返す' do
+          put :update, :id => @story.id, :story => @attr, :format => :json
+          response.message.should match(/Unprocessable/)
+        end
+      end
+    end
   end
 
 end