OSDN Git Service

add test on comic.play
[pettanr/pettanr.git] / spec / controllers / comics_controller_spec.rb
index 40446c2..7d93e76 100644 (file)
@@ -23,6 +23,22 @@ describe ComicsController do
         get :index
         assigns(:page).should eq 1
       end
+      it '与えられたpage_sizeがセットされている' do
+        get :index, :page_size => 15
+        assigns(:page_size).should eq 15
+      end
+      it '省略されると@page_sizeにデフォルト値が入る' do
+        get :index
+        assigns(:page_size).should eq Comic.default_page_size\r
+      end
+      it '最大を超えると@page_sizeにデフォルト最大値が入る' do
+        get :index, :page_size => 1500
+        assigns(:page_size).should eq Comic.max_page_size
+      end
+      it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
+        get :index, :page_size => 0
+        assigns(:page_size).should eq Comic.default_page_size
+      end
     end
     context 'つつがなく終わるとき' do
       it 'ステータスコード200 OKを返す' do
@@ -188,6 +204,110 @@ describe ComicsController do
     end
 =end
   end
+  describe '閲覧に於いて' do
+    before do
+      @comic = Factory :normal_comic, :author_id => @user.author.id
+      Comic.stub(:show).and_return(@comic)
+      sign_in @user
+    end
+    context '事前チェックする' do
+      before do
+        Panel.stub(:count).and_return(10)
+      end
+      it '与えられたoffsetがセットされている' do
+        get :play, :id => @comic.id, :offset => 7
+        assigns(:offset).should eq 7
+      end
+      it '省略されると@offsetに0値が入る' do
+        get :play, :id => @comic.id
+        assigns(:offset).should eq 0
+      end
+      it 'コマ数以上が与えられるとコマ数-1が入る' do
+        get :play, :id => @comic.id, :offset => 10
+        assigns(:offset).should eq 9
+      end
+      it '負の値が与えられると末尾(コマ数)からさかのぼった値が入る' do
+        get :play, :id => @comic.id, :offset => -3
+        assigns(:offset).should eq 7
+      end
+      it 'コマ数以上の負の値が与えられると計算できないので0値が入る' do
+        get :play, :id => @comic.id, :offset => -13
+        assigns(:offset).should eq 0
+      end
+      it '与えられたcountがセットされている' do
+        get :play, :id => @comic.id, :count => 18
+        assigns(:count).should eq 18
+      end
+      it '省略されると@countにデフォルト値が入る' do
+        get :play, :id => @comic.id
+        assigns(:count).should eq Comic.default_panel_size\r
+      end
+      it '最大を超えると@countにデフォルト最大値が入る' do
+        get :play, :id => @comic.id, :count => 1500
+        assigns(:count).should eq Comic.max_panel_size
+      end
+      it '不正な値が入ると@countにデフォルト最大値が入る' do
+        get :play, :id => @comic.id, :page_size => 0
+        assigns(:count).should eq Comic.default_panel_size
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :play, :id => @comic.id
+        response.should be_success
+      end
+      it 'コミックモデルに単体取得を問い合わせている' do
+        Comic.should_receive(:show).exactly(1)\r
+        get :play, :id => @comic.id
+      end
+      it '@comicにアレを取得している' do
+        get :play, :id => @comic.id
+        assigns(:comic).id.should eq(@comic.id)
+      end
+      context 'html形式' do
+        it 'playテンプレートを描画する' do
+          get :play, :id => @comic.id
+          response.should render_template("play")
+        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)
+        end
+        it 'データがアレになっている' do
+          get :play, :id => @comic.id, :format => :json
+          json = JSON.parse response.body
+          json["title"].should match(/normal/)
+        end
+      end
+    end
+    context '作家権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :play, :id => @comic.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :play, :id => @comic.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :play, :id => @comic.id, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :play, :id => @comic.id, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+  end
 
   describe 'コミック数取得に於いて' do
     before do