OSDN Git Service

merge v06sheet
[pettanr/pettanr.git] / spec / controllers / sheets_controller_spec.rb
diff --git a/spec/controllers/sheets_controller_spec.rb b/spec/controllers/sheets_controller_spec.rb
new file mode 100644 (file)
index 0000000..289f3ed
--- /dev/null
@@ -0,0 +1,1412 @@
+# -*- encoding: utf-8 -*-
+require 'spec_helper'
+#用紙
+describe SheetsController do
+  before do
+    @admin =FactoryGirl.create :admin
+    @sp = FactoryGirl.create :system_picture
+    @lg = FactoryGirl.create :license_group
+    @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+    @user = FactoryGirl.create :user_yas
+    @author = FactoryGirl.create :author, :user_id => @user.id
+  end
+  
+if MagicNumber['run_mode'] == 1
+  describe '一覧表示に於いて' do
+    before do
+      @sheet = FactoryGirl.create :sheet, :author_id => @user.author.id
+      Sheet.stub(:list).and_return([@sheet, @sheet, @sheet])
+      sign_in @user
+    end
+    context '事前チェックする' do
+      it '与えられたpageがセットされている' do
+        get :index, :page => 5
+        assigns(:page).should eq 5
+      end
+      it '省略されると@pageに1値が入る' 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 Sheet.default_page_size
+      end
+      it '最大を超えると@page_sizeにデフォルト最大値が入る' do
+        get :index, :page_size => 1500
+        assigns(:page_size).should eq Sheet.max_page_size
+      end
+      it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
+        get :index, :page_size => 0
+        assigns(:page_size).should eq Sheet.default_page_size
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
+      end
+      it '用紙モデルに一覧を問い合わせている' do
+        Sheet.should_receive(:list).exactly(1)
+        get :index
+      end
+      it '@sheetsにリストを取得している' do
+        get :index
+        assigns(:sheets).should have_at_least(3).items
+      end
+      context 'html形式' do
+        it '@paginateにページ制御を取得している' do
+          get :index
+          assigns(:paginate).is_a?(Kaminari::PaginatableArray).should be_true
+        end
+        it 'indexテンプレートを描画する' do
+          get :index
+          response.should render_template("index")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :index, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it '用紙モデルにjson一覧出力オプションを問い合わせている' do
+          Sheet.should_receive(:list_json_opt).exactly(1)
+          get :index, :format => :json
+        end
+        it 'データがリスト構造になっている' do
+          get :index, :format => :json
+          json = JSON.parse response.body
+          json.should have_at_least(3).items
+        end
+        it 'リストの先頭くらいは用紙っぽいものであって欲しい' do
+          get :index, :format => :json
+          json = JSON.parse response.body
+          json.first.has_key?("caption").should be_true
+          json.first.has_key?("visible").should be_true
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :index
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :index
+          response.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :index, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :index, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context 'ユーザ権限はないが管理者権限があるとき' do
+      before do
+        sign_out @user
+        sign_in @admin
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :index
+          response.should be_success 
+        end
+      end
+    end
+  end
+  
+  describe '単体表示に於いて' do
+    before do
+      @sheet = FactoryGirl.create :sheet, :author_id => @user.author.id, :caption => 'normal'
+      Sheet.stub(:show).and_return(@sheet)
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @sheet.id
+        response.should be_success
+      end
+      it '用紙モデルに単体取得を問い合わせている' do
+        Sheet.should_receive(:show).exactly(1)
+        get :show
+      end
+      it '@sheetにアレを取得している' do
+        get :show, :id => @sheet.id
+        assigns(:sheet).id.should eq(@sheet.id)
+      end
+      context 'html形式' do
+        it 'showテンプレートを描画する' do
+          get :show, :id => @sheet.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @sheet.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it '用紙モデルにjson単体出力オプションを問い合わせている' do
+          Sheet.should_receive(:show_json_opt).exactly(1)
+          get :show, :id => @sheet.id, :format => :json
+        end
+        it 'データがアレになっている' do
+          get :show, :id => @sheet.id, :format => :json
+          json = JSON.parse response.body
+          json["caption"].should match(/normal/)
+          json["visible"].should_not be_nil
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :show, :id => @sheet.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :show, :id => @sheet.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :show, :id => @sheet.id, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :show, :id => @sheet.id, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context 'ユーザ権限はないが管理者権限があるとき' do
+      before do
+        sign_out @user
+        sign_in @admin
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @sheet.id
+        response.should be_success 
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :show, :id => @sheet.id
+          response.should be_success
+        end
+      end
+    end
+=begin
+    context '対象用紙がないとき' do
+      context 'html形式' do
+        it '例外404 not_foundを返す' do
+          lambda{
+            get :show, :id => 0
+          }.should raise_error(ActiveRecord::RecordNotFound)
+        end
+      end
+      context 'json形式' do
+        it '例外404 not_foundを返す' do
+          lambda{ 
+            get :show, :id => 0, :format => :json
+          }.should raise_error(ActiveRecord::RecordNotFound)
+        end
+      end
+    end
+    context '非公開用紙を見ようとしたとき' do
+      context 'html形式' do
+        it '例外403 forbiddenを返す' do
+          Sheet.any_instance.stub(:visible?).with(any_args()).and_return(false)
+          hidden = FactoryGirl.create :hidden_sheet, :author_id => @author.id
+          lambda{
+            get :show, :id => hidden
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+      context 'json形式' do
+        it '例外403 forbiddenを返す' do
+          Sheet.any_instance.stub(:visible?).with(any_args()).and_return(false)
+          hidden = FactoryGirl.create :hidden_sheet, :author_id => @author.id
+          lambda{
+            get :show, :id => hidden, :format => :json
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+    end
+=end
+  end
+  
+  describe '閲覧に於いて' do
+    before do
+      @sheet = FactoryGirl.create :sheet, :author_id => @user.author.id, :caption => 'normal'
+      @panel = FactoryGirl.create :panel, :author_id => @author.id
+      @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+      Sheet.stub(:show).with(@sheet.id.to_s, [@user, nil]).and_return(@sheet)
+      Sheet.stub(:show).with(@sheet.id.to_s, [nil, @admin]).and_return(@sheet)
+      SheetPanel.stub(:count).and_return(10)
+      SheetPanel.stub(:play_list).with(any_args).and_return([@sheet_panel, @sheet_panel, @sheet_panel])
+      sign_in @user
+    end
+    context '事前チェックする' do
+      it '用紙モデルに単体取得を問い合わせている' do
+        Sheet.should_receive(:show).with(@sheet.id.to_s, [@user, nil]).exactly(1)
+        get :play, :id => @sheet.id
+      end
+    end
+    context 'つつがなく終わるとき' do
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :play, :id => @sheet.id
+          response.should be_success 
+        end
+        it 'sheetテンプレートを描画する' do
+          get :play, :id => @sheet.id
+          response.should render_template("sheet")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :play, :id => @sheet.id, :format => :json
+          response.should be_success 
+        end
+        it 'jsonデータを返す' do
+          get :play, :id => @sheet.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it '用紙モデルにjson単体出力オプションを問い合わせている' do
+          Sheet.should_receive(:show_json_opt).exactly(1)
+          get :show, :id => @sheet.id, :format => :json
+        end
+        it 'データがアレになっている' do
+          get :play, :id => @sheet.id, :format => :json
+          json = JSON.parse response.body
+          json["caption"].should match(/normal/)
+          json["visible"].should_not be_nil
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :play, :id => @sheet.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :play, :id => @sheet.id
+          response.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :play, :id => @sheet.id, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :play, :id => @sheet.id, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context 'ユーザ権限はないが管理者権限があるとき' do
+      before do
+        sign_out @user
+        sign_in @admin
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :play, :id => @sheet.id
+        response.should be_success 
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :play, :id => @sheet.id
+        response.should be_success 
+      end
+    end
+  end
+
+  describe '用紙数取得に於いて' do
+    before do
+      Sheet.should_receive(:visible_count).and_return(3)
+#      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :count, :format => :json
+        response.should be_success 
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :count, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがHash構造になっていて用紙数が1である' do
+          get :count, :format => :json
+          json = JSON.parse response.body
+          json["count"].should == 3
+        end
+      end
+    end
+  end
+
+  describe '新規作成フォーム表示に於いて' do
+    before do
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :new
+        response.should be_success 
+      end
+      it '@sheetに新規データを用意している' do
+        get :new
+        assigns(:sheet).should be_a_new(Sheet)
+      end
+      it '用紙モデルにデフォルト値補充を依頼している' do
+        Sheet.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
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :new, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it '用紙モデルにjson単体出力オプションを問い合わせている' do
+          Sheet.should_receive(:show_json_opt).exactly(1)
+          get :new, :format => :json
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      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
+      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
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :new, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :new, :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
+          get :new
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :new
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :new, @attr
+          response.status.should eq 302
+        end
+        it '作家登録ページへ遷移する' do
+          get :new, @attr
+          response.body.should redirect_to new_author_path
+        end
+      end
+    end
+  end
+
+  describe '新規作成に於いて' do
+    before do
+      sign_in @user
+      @attr = FactoryGirl.attributes_for(:sheet, :author_id => @author.id, :caption => 'normal')
+    end
+    context '事前チェックしておく' do
+      it 'sheetがパラメータにあれば、展開する' do
+        post :create, :sheet => @attr
+        assigns(:prm)['caption'].should eq 'normal'
+      end
+      it 'jsonがパラメータにあれば、展開する' do
+        post :create, :json => FactoryGirl.attributes_for(:sheet, :caption => 'json prm').to_json
+        assigns(:prm)['caption'].should eq 'json prm'
+      end
+      it 'sheet・json両パラメータがあれば、sheetを優先して展開する' do
+        post :create, {
+          :sheet => FactoryGirl.attributes_for(:sheet, :caption => 'normal'), 
+          :json => FactoryGirl.attributes_for(:sheet, :caption => 'json prm').to_json
+        }
+        assigns(:prm)['caption'].should eq 'normal'
+      end
+      it '用紙モデルにデフォルト値補充を依頼している' do
+        Sheet.any_instance.should_receive(:supply_default).exactly(1)
+        post :create, :sheet => @attr
+      end
+      it 'モデルに保存依頼する' do
+        Sheet.any_instance.should_receive(:store).exactly(1)
+        post :create, :sheet => @attr
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it "@sheetに作成された用紙を保持していて、それがDBにある" do
+        post :create, :sheet => @attr
+        assigns(:sheet).should be_a(Sheet)
+        assigns(:sheet).should be_persisted
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          Sheet.any_instance.stub(:store).and_return(true)
+          post :create, :sheet => @attr
+          response.status.should eq 302
+        end
+        it '作成された用紙の表示ページへ遷移する' do
+#          Sheet.any_instance.stub(:store).and_return(true)
+          post :create, :sheet => @attr
+          response.should redirect_to(Sheet.last)
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+#          Sheet.any_instance.stub(:store).and_return(true)
+          post :create, :sheet => @attr, :format => :json
+          response.should be_success 
+        end
+        it '作成された用紙をjsonデータで返す' do
+          post :create, :sheet => @attr, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがアレになっている' do
+          post :create, :sheet => @attr, :format => :json
+          json = JSON.parse response.body
+          json["caption"].should match(/normal/)
+          json["visible"].should_not be_nil
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          post :create, :sheet => @attr
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          post :create, :sheet => @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          post :create, :sheet => @attr, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          post :create, :sheet => @attr, :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
+          post :create, :sheet => @attr
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          post :create, :sheet => @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          post :create, :sheet => @attr
+          response.status.should eq 302
+        end
+        it '作家登録ページへ遷移する' do
+          post :create, :sheet => @attr
+          response.body.should redirect_to new_author_path
+        end
+      end
+    end
+    context '検証、保存に失敗した' do
+      before do
+        Sheet.any_instance.stub(:save).and_return(false)
+      end
+      it "未保存の用紙を保持している" do
+        post :create, :sheet => @attr
+        assigns(:sheet).should be_a_new(Sheet)
+      end
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          post :create, :sheet => @attr
+          response.status.should eq 200
+        end
+        it '新規ページを描画する' do
+          post :create, :sheet => @attr
+          response.should render_template("new")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード422 unprocessable_entity を返す' do
+          post :create, :sheet => @attr, :format => :json
+          response.status.should eq 422
+        end
+        it '応答メッセージUnprocessable Entityを返す' do
+          post :create, :sheet => @attr, :format => :json
+          response.message.should match(/Unprocessable/)
+        end
+      end
+    end
+  end
+
+  describe '編集フォーム表示に於いて' do
+    before do
+      @sheet = FactoryGirl.create :sheet, :author_id => @user.author.id
+      sign_in @user
+      Sheet.stub(:edit).and_return(@sheet)
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :edit, :id => @sheet.id
+        response.should be_success 
+      end
+      it '用紙モデルに編集取得を問い合わせている' do
+        Sheet.should_receive(:edit).exactly(1)
+        get :edit, :id => @sheet.id
+      end
+      it '@sheetにデータを用意している' do
+        get :edit, :id => @sheet.id
+        assigns(:sheet).should eq @sheet
+      end
+      context 'html形式' do
+        it 'editテンプレートを描画する' do
+          get :edit, :id => @sheet.id
+          response.should render_template("edit")
+        end
+      end
+      context 'js形式' do
+        it 'edit.jsテンプレートを描画する' do
+          get :edit, :id => @sheet.id, :format => :js
+          response.should render_template("edit")
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :edit, :id => @sheet.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :edit, :id => @sheet.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'js形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :edit, :id => @sheet.id, :format => :js
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :edit, :id => @sheet.id, :format => :js
+          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
+          get :edit, :id => @sheet.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :edit, :id => @sheet.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :edit, :id => @sheet.id
+          response.status.should eq 302
+        end
+        it '作家登録ページへ遷移する' do
+          get :edit, :id => @sheet.id
+          response.body.should redirect_to new_author_path
+        end
+      end
+    end
+  end
+
+  describe '更新に於いて' do
+    before do
+      @sheet = FactoryGirl.create :sheet, :author => @author
+      @attr = FactoryGirl.attributes_for(:sheet, :author_id => @author.id, :caption => 'updated caption', :visible => 0)
+      sign_in @user
+    end
+    context '事前チェックしておく' do
+      it 'sheetがパラメータにあれば、展開する' do
+        put :update, :id => @sheet.id, :sheet => @attr
+        assigns(:prm)['caption'].should eq 'updated caption'
+      end
+      it 'jsonがパラメータにあれば、展開する' do
+        put :update, :id => @sheet.id, :json => FactoryGirl.attributes_for(:sheet, :caption => 'json caption').to_json
+        assigns(:prm)['caption'].should eq 'json caption'
+      end
+      it 'sheet・json両パラメータがあれば、sheetを優先して展開する' do
+        put :update, {
+          :id => @sheet.id, 
+          :sheet => FactoryGirl.attributes_for(:sheet, :caption => 'updated caption'), 
+          :json => FactoryGirl.attributes_for(:sheet, :caption => 'json caption').to_json
+        }
+        assigns(:prm)['caption'].should eq 'updated caption'
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it '用紙モデルに編集取得を問い合わせている' do
+        Sheet.stub(:edit).with(any_args()).and_return @sheet
+        Sheet.should_receive(:edit).exactly(1)
+        put :update, :id => @sheet.id, :sheet => @attr
+      end
+      it 'モデルに更新を依頼する' do
+        Sheet.any_instance.stub(:store).with(any_args).and_return true
+        Sheet.any_instance.should_receive(:store).exactly(1)
+        put :update, :id => @sheet.id, :sheet => @attr
+      end
+      it '@sheetにアレを取得している' do
+        put :update, :id => @sheet.id, :sheet => @attr
+        assigns(:sheet).id.should eq(@sheet.id)
+      end
+      it '更新される' do
+        put :update, :id => @sheet.id, :sheet => @attr
+        Sheet.find(@sheet.id).visible.should eq 0
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          Sheet.any_instance.stub(:store).with(any_args()).and_return(true)
+          put :update, :id => @sheet.id, :sheet => @attr
+          response.status.should eq 302
+        end
+        it '更新された用紙の表示ページへ遷移する' do
+          put :update, :id => @sheet.id, :sheet => @attr
+          response.should redirect_to(@sheet)
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          Sheet.any_instance.stub(:store).with(any_args()).and_return(true)
+          put :update, :id => @sheet.id, :sheet => @attr, :format => :json
+          response.should be_success 
+        end
+        it 'ページ本体は特に返さない' do
+          Sheet.any_instance.stub(:store).with(any_args()).and_return(true)
+          put :update, :id => @sheet.id, :sheet => @attr, :format => :json
+          response.body.should match /./
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          put :update, :id => @sheet.id, :sheet => @attr
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          put :update, :id => @sheet.id, :sheet => @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it '応答メッセージにUnauthorizedを返す' do
+          put :update, :id => @sheet.id, :sheet => @attr, :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
+          put :update, :id => @sheet.id, :sheet => @attr
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          put :update, :id => @sheet.id, :sheet => @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          put :update, :id => @sheet.id, :sheet => @attr
+          response.status.should eq 302
+        end
+        it '作家登録ページへ遷移する' do
+          put :update, :id => @sheet.id, :sheet => @attr
+          response.body.should redirect_to new_author_path
+        end
+      end
+    end
+    context '検証、保存に失敗したとき' do
+      before do
+        Sheet.any_instance.stub(:save).and_return(false)
+      end
+      context 'html形式' do
+        it 'ステータスコード200 Okを返す' do
+          put :update, :id => @sheet.id, :sheet => @attr
+          response.status.should eq 200
+        end
+        it '編集ページを描画する' do
+          put :update, :id => @sheet.id, :sheet => @attr
+          response.should render_template("edit")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード422 unprocessable_entity を返す' do
+          Sheet.any_instance.stub(:save).and_return(false)
+          put :update, :id => @sheet.id, :sheet => @attr, :format => :json
+          response.status.should eq 422
+        end
+        it '応答メッセージUnprocessable Entityを返す' do
+          put :update, :id => @sheet.id, :sheet => @attr, :format => :json
+          response.message.should match(/Unprocessable/)
+        end
+      end
+    end
+  end
+
+  describe '削除に於いて' do
+    before do
+      @sheet = FactoryGirl.create :sheet, :author => @author
+      sign_in @user
+    end
+    context '事前チェックしておく' do
+      before do
+        Sheet.stub(:edit).with(any_args()).and_return @sheet
+        Sheet.any_instance.stub(:destroy_with_sheet_panel).with(any_args()).and_return(true)
+      end
+      it '用紙モデルに編集取得を問い合わせている' do
+        Sheet.should_receive(:edit).exactly(1)
+        delete :destroy, :id => @sheet.id
+      end
+      it 'モデルに削除を依頼する' do
+        Sheet.any_instance.should_receive(:destroy_with_sheet_panel).exactly(1)
+        delete :destroy, :id => @sheet.id
+      end
+      it '@sheetにアレを取得している' do
+        delete :destroy, :id => @sheet.id
+        assigns(:sheet).id.should eq(@sheet.id)
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it '削除される' do
+        lambda {
+          delete :destroy, :id => @sheet.id
+        }.should change Sheet, :count
+      end
+      context 'html形式' do
+        before do
+          Sheet.any_instance.stub(:destroy_with_sheet_panel).with(any_args()).and_return(true)
+        end
+        it 'ステータスコード302 Foundを返す' do
+          delete :destroy, :id => @sheet.id
+          response.status.should eq 302
+        end
+        it 'マイ用紙の一覧ページへ遷移する' do
+          delete :destroy, :id => @sheet.id
+          response.should redirect_to('/home/sheets')
+        end
+      end
+      context 'json形式' do
+        before do
+          Sheet.any_instance.stub(:destroy_with_sheet_panel).with(any_args()).and_return(true)
+        end
+        it 'ステータスコード200 OKを返す' do
+          delete :destroy, :id => @sheet.id, :format => :json
+          response.should be_success 
+        end
+        it 'ページ本体は特に返さない' do
+          delete :destroy, :id => @sheet.id, :format => :json
+          response.body.should match /./
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          delete :destroy, :id => @sheet.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          delete :destroy, :id => @sheet.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it '応答メッセージにUnauthorizedを返す' do
+          delete :destroy, :id => @sheet.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 => @sheet.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          delete :destroy, :id => @sheet.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          delete :destroy, :id => @sheet.id
+          response.status.should eq 302
+        end
+        it '作家登録ページへ遷移する' do
+          delete :destroy, :id => @sheet.id
+          response.body.should redirect_to new_author_path
+        end
+      end
+    end
+    context '削除に失敗したとき' do
+      before do
+        Sheet.any_instance.stub(:destroy_with_sheet_panel).and_return(false)
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          delete :destroy, :id => @sheet.id
+          response.status.should eq 302
+        end
+        it 'その用紙の詳細ページへ遷移する' do
+          delete :destroy, :id => @sheet.id
+          response.should redirect_to(sheet_path(@sheet))
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード422 unprocessable_entity を返す' do
+          delete :destroy, :id => @sheet.id, :format => :json
+          response.status.should eq 422
+        end
+        it '応答メッセージUnprocessable Entityを返す' do
+          delete :destroy, :id => @sheet.id, :format => :json
+          response.message.should match(/Unprocessable/)
+        end
+      end
+    end
+  end
+
+else
+  describe '一覧表示に於いて' do
+    before do
+      @sheet = FactoryGirl.create :sheet, :author_id => @user.author.id
+      Sheet.stub(:list).and_return([@sheet, @sheet, @sheet])
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
+      end
+      context 'html形式' do
+        it 'indexテンプレートを描画する' do
+          get :index
+          response.should render_template("index")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :index, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
+      end
+      context 'html形式' do
+        it 'indexテンプレートを描画する' do
+          get :index
+          response.should render_template("index")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :index, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+  end
+  
+  describe '単体表示に於いて' do
+    before do
+      @sheet = FactoryGirl.create :sheet, :author_id => @user.author.id, :caption => 'normal'
+      Sheet.stub(:show).and_return(@sheet)
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @sheet.id
+        response.should be_success
+      end
+      context 'html形式' do
+        it 'showテンプレートを描画する' do
+          get :show, :id => @sheet.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @sheet.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @sheet.id
+        response.should be_success
+      end
+      context 'html形式' do
+        it 'showテンプレートを描画する' do
+          get :show, :id => @sheet.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @sheet.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+  end
+  describe '用紙数取得に於いて' do
+    before do
+      Sheet.should_receive(:visible_count).and_return(3)
+      sign_out @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :count, :format => :json
+        response.should be_success 
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :count, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+  end
+
+  describe '新規作成フォーム表示に於いて' do
+    before do
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :new
+        response.should be_success 
+      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
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :new, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      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
+      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
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :new, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :new, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+  end
+
+  describe '新規作成に於いて' do
+    before do
+      sign_in @user
+      @attr = FactoryGirl.attributes_for(:sheet, :author_id => @author.id, :caption => 'normal')
+    end
+    context 'つつがなく終わるとき' do
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          Sheet.any_instance.stub(:save).and_return(true)
+          post :create, :sheet => @attr
+          response.status.should eq 302
+        end
+        it '作成された用紙の表示ページへ遷移する' do
+#          Sheet.any_instance.stub(:save).and_return(true)
+          post :create, :sheet => @attr
+          response.should redirect_to(Sheet.last)
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+#          Sheet.any_instance.stub(:save).and_return(true)
+          post :create, :sheet => @attr, :format => :json
+          response.should be_success 
+        end
+        it '作成された用紙をjsonデータで返す' do
+          post :create, :sheet => @attr, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          post :create, :sheet => @attr
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          post :create, :sheet => @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          post :create, :sheet => @attr, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          post :create, :sheet => @attr, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+  end
+
+  describe '編集フォーム表示に於いて' do
+    before do
+      @sheet = FactoryGirl.create :sheet, :author_id => @user.author.id
+      sign_in @user
+      Sheet.stub(:edit).and_return(@sheet)
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :edit, :id => @sheet.id
+        response.should be_success 
+      end
+      context 'html形式' do
+        it 'editテンプレートを描画する' do
+          get :edit, :id => @sheet.id
+          response.should render_template("edit")
+        end
+      end
+      context 'js形式' do
+        it 'edit.jsテンプレートを描画する' do
+          get :edit, :id => @sheet.id, :format => :js
+          response.should render_template("edit")
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :edit, :id => @sheet.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :edit, :id => @sheet.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'js形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :edit, :id => @sheet.id, :format => :js
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :edit, :id => @sheet.id, :format => :js
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+  end
+
+  describe '更新に於いて' do
+    before do
+      @sheet = FactoryGirl.create :sheet, :author => @author
+      @attr = FactoryGirl.attributes_for(:sheet, :author_id => @author.id, :caption => 'updated caption', :visible => 0)
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          Sheet.any_instance.stub(:save).with(any_args()).and_return(true)
+          put :update, :id => @sheet.id, :sheet => @attr
+          response.status.should eq 302
+        end
+        it '更新された用紙の表示ページへ遷移する' do
+          put :update, :id => @sheet.id, :sheet => @attr
+          response.should redirect_to(@sheet)
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          Sheet.any_instance.stub(:save).with(any_args()).and_return(true)
+          put :update, :id => @sheet.id, :sheet => @attr, :format => :json
+          response.should be_success 
+        end
+        it 'ページ本体は特に返さない' do
+          Sheet.any_instance.stub(:save).with(any_args()).and_return(true)
+          put :update, :id => @sheet.id, :sheet => @attr, :format => :json
+          response.body.should match /./
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      it 'ステータスコード302 Foundを返す' do
+        put :update, :id => @sheet.id, :sheet => @attr
+        response.status.should eq 302
+      end
+      context 'html形式' do
+        it 'サインインページへ遷移する' do
+          put :update, :id => @sheet.id, :sheet => @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+    end
+  end
+
+  describe '削除に於いて' do
+    before do
+      @sheet = FactoryGirl.create :sheet, :author => @author
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      context 'html形式' do
+        before do
+          Sheet.any_instance.stub(:destroy_with_sheet_panel).with(any_args()).and_return(true)
+        end
+        it 'ステータスコード302 Foundを返す' do
+          delete :destroy, :id => @sheet.id
+          response.status.should eq 302
+        end
+        it 'マイ用紙の一覧ページへ遷移する' do
+          delete :destroy, :id => @sheet.id
+          response.should redirect_to('/home/sheet')
+        end
+      end
+      context 'json形式' do
+        before do
+          Sheet.any_instance.stub(:destroy_with_sheet_panel).with(any_args()).and_return(true)
+        end
+        it 'ステータスコード200 OKを返す' do
+          delete :destroy, :id => @sheet.id, :format => :json
+          response.should be_success 
+        end
+        it 'ページ本体は特に返さない' do
+          delete :destroy, :id => @sheet.id, :format => :json
+          response.body.should match /./
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      it 'ステータスコード302 Foundを返す' do
+        delete :destroy, :id => @sheet.id
+        response.status.should eq 302
+      end
+      context 'html形式' do
+        it 'サインインページへ遷移する' do
+          delete :destroy, :id => @sheet.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it '応答メッセージにUnauthorizedを返す' do
+          delete :destroy, :id => @sheet.id, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+  end
+
+end
+
+end