From 3e08dd3b1d0b84f2ad76c9a53bf6cad8b046ec62 Mon Sep 17 00:00:00 2001 From: yasushiito Date: Wed, 2 Jul 2014 08:11:01 +0900 Subject: [PATCH] fix test --- app/controllers/application_controller.rb | 42 +- app/controllers/scrolls_controller.rb | 14 +- lib/peta/binder.rb | 6 +- spec/controllers/scrolls_controller_spec.rb | 683 +++++++++++++++------------- spec/support/controller_macros.rb | 114 +++-- 5 files changed, 470 insertions(+), 389 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 98a8b8a9..38e5c7f3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -323,7 +323,7 @@ class ApplicationController < ActionController::Base } end - def leaf_not_update_html_format format, redirect_url + def leaf_not_updated_html_format format, redirect_url format.html { flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human) redirect_to redirect_url @@ -344,6 +344,46 @@ class ApplicationController < ActionController::Base end end + def destroyed_html_format format, redirect_url + format.html { + flash[:notice] = I18n.t('flash.notice.destroyed', :model => @my_model_class.model_name.human) + redirect_to redirect_url + } + end + + def destroyed_json_format format + format.json { + head :ok + } + end + + def not_destroyed_html_format format + format.html { + flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => @my_model_class.model_name.human) + redirect_to @item + } + end + + def not_destroyed_json_format format + format.json { + render json: @item.errors, status: :unprocessable_entity + } + end + + def render_destroy redirect_url = nil + if @item.destroy + respond_to do |format| + destroyed_html_format format, redirect_url + destroyed_json_format format + end + else + respond_to do |format| + not_destroyed_html_format format + not_destroyed_json_format format + end + end + end + def list_count set_list j = {:count => @list.count(@operators, {:id => params[:id]})} diff --git a/app/controllers/scrolls_controller.rb b/app/controllers/scrolls_controller.rb index 3b59b72b..cdd9aa64 100644 --- a/app/controllers/scrolls_controller.rb +++ b/app/controllers/scrolls_controller.rb @@ -4,7 +4,7 @@ class ScrollsController < ApplicationController before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy] else before_filter :authenticate_reader, :only => [ - :top, :index, :show, :play, :by_panel, :by_author, :count, :count_by_panel, :count_by_author + :top, :index, :show, :play, :by_panel, :by_author ] before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy] before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy] @@ -98,16 +98,6 @@ class ScrollsController < ApplicationController def destroy set_model @item = @my_model_class.edit(params[:id], @operators) - respond_to do |format| - if @item.destroy_with_leafs - flash[:notice] = I18n.t('flash.notice.destroyed', :model => Scroll.model_name.human) - format.html { redirect_to '/home/scrolls' } - format.json { head :ok } - else - flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Scroll.model_name.human) - format.html { redirect_to @item } - format.json { render json: @item.errors, status: :unprocessable_entity } - end - end + render_destroy '/home/' + @item.path_name end end diff --git a/lib/peta/binder.rb b/lib/peta/binder.rb index 76872dad..efedd650 100644 --- a/lib/peta/binder.rb +++ b/lib/peta/binder.rb @@ -70,14 +70,14 @@ module Peta self.class.validate_elements_serial validate_serial_list end - def destroy_with_leafs + def destroy res = false self.class.transaction do self.leafs_items.each do |leaf| raise ActiveRecord::Rollback unless leaf.destroy_and_shorten end - raise ActiveRecord::Rollback unless self.destroy - res = true + res = super + raise ActiveRecord::Rollback unless res end res end diff --git a/spec/controllers/scrolls_controller_spec.rb b/spec/controllers/scrolls_controller_spec.rb index bd76f306..a41b9672 100644 --- a/spec/controllers/scrolls_controller_spec.rb +++ b/spec/controllers/scrolls_controller_spec.rb @@ -7,154 +7,201 @@ describe ScrollsController do @user_yas = FactoryGirl.create :user_yas_with_owner @user_rom = FactoryGirl.create :user_rom @author_yas = @user_yas.author + + @my_model_class = Scroll end if Manifest.manifest.magic_numbers['run_mode'] == 1 describe '一覧表示に於いて' do before do factory_contents :scroll_hello, @author_yas + @query = { } + @query_format_json = @query.merge(:format => :json) end - context '正常系の共通処理' do + context '正常系' do before do sign_in @user_yas end - it 'ステータス OKを返して、ファイラーまたはjsonデータを返す。' do - list_should_return_filer_or_json :index - end - it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do - get :index - expect(assigns(:my_model_class)).to be Scroll - end - it 'リストグループのマニフェストから新着リストの取得処理を@listに取得している' do - get :index - expect(assigns(:list).item_name).to eq('scroll') - expect(assigns(:list).list_name).to eq('public') - end - it '@itemsにリストを取得している' do - get :index - expect(assigns(:items).size).to be >= 3 + context '共通処理' do + before do + get :index, @query + end + it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do + set_model? @my_model_class + end + it 'リストグループのマニフェストから新着リストの取得処理を@listに取得している' do + set_list? 'scroll', 'public' + end + it '@itemsにリストを取得している' do + expect(assigns(:items).size).to be >= 3 + end end context 'html形式' do + before do + get :index, @query + end + it 'ステータス OKを返して、ファイラーを描画している' do + return_ok? + render_filer? + end it '@filerにファイラーを取得している' do - get :index - expect(assigns(:filer).class).to be Locmare::Filer + let_filer? end end context 'json形式' do + before do + get :index, @query_format_json + end + it 'ステータス OKを返して、jsonデータを返している' do + return_ok? + render_json? + end it '@filerにファイラーをセットしていない' do - get :index, format: :json expect(assigns(:filer)).to be nil end end end context '件数取得' do - before do - sign_in @user_yas - end - it 'ステータス OKを返す' do - return_ok_at_get :count, format: :json - end context 'html形式' do it '必要ないので実装していない' do - get :count + sign_in @user_yas + get :count, @query expect(response.status).to eq(406) end end context 'json形式' do - it 'jsonデータを返す' do - get :count, format: :json - expect {JSON.parse(response.body)}.not_to raise_error + it 'ステータス OKを返して、jsonデータを返している' do + sign_in @user_yas + get :count, @query_format_json + return_ok? + render_json? + end + end + context 'ゲスト(サインインしていない)' do + it '通常通り応答する' do + get :count, @query_format_json + return_ok? + render_json? end end end context 'サインインの状態が例外的なとき' do - it 'ユーザではなく管理者でも通常通り応答する' do + it 'ユーザではなく管理者でも通常通り応答している' do sign_in @admin - return_ok_at_get :index - return_ok_at_get :index, format: :json + get :index, @query + return_ok? + get :index, @query_format_json + render_json? end - it '創作活動してないROM専の読者にも通常通り応答する' do + it '創作活動してないROM専の読者にも通常通り応答している' do sign_in @user_rom - return_ok_at_get :index - return_ok_at_get :index, format: :json + get :index, @query + return_ok? + get :index, @query_format_json + render_json? end - it 'ゲスト(サインインしていない)はサインインページに遷移する' do - announce_sign_in_at_get :index + it 'ゲスト(サインインしていない)はサインインページに遷移している' do + get :index, @query + redirect? sign_in_url + get :index, @query_format_json + render_unauthorized? end end end describe '作家フィルタ一覧及びカウント' do before do factory_contents :scroll_hello, @author_yas + @query = {:id => @author_yas.id} + @query_format_json = @query.merge(:format => :json) end - context '正常系の共通処理' do + context '正常系' do before do sign_in @user_yas end - it 'ステータス OKを返して、ファイラーまたはjsonデータを返す。' do - list_should_return_filer_or_json :by_author, id: @author_yas.id - end - it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do - get :by_author, id: @author_yas.id - expect(assigns(:my_model_class)).to be Scroll - end - it 'リストグループのマニフェストから新着リストの取得処理を@listに取得している' do - get :by_author, id: @author_yas.id - expect(assigns(:list).item_name).to eq('scroll') - expect(assigns(:list).list_name).to eq('by_author') - end - it '@itemsにリストを取得している' do - get :by_author, id: @author_yas.id - expect(assigns(:items).size).to be >= 3 + context '共通処理' do + before do + get :by_author, @query + end + it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do + set_model? @my_model_class + end + it 'リストグループのマニフェストから新着リストの取得処理を@listに取得している' do + set_list? 'scroll', 'by_author' + end + it '@itemsにリストを取得している' do + expect(assigns(:items).size).to be >= 3 + end end context 'html形式' do + before do + get :by_author, @query + end + it 'ステータス OKを返して、ファイラーを描画している' do + return_ok? + render_filer? + end it '@filerにファイラーを取得している' do - get :by_author, id: @author_yas.id - expect(assigns(:filer).class).to be Locmare::Filer + let_filer? end end context 'json形式' do + before do + get :by_author, @query_format_json + end + it 'ステータス OKを返して、jsonデータを返している' do + return_ok? + render_json? + end it '@filerにファイラーをセットしていない' do - get :by_author, id: @author_yas.id, format: :json expect(assigns(:filer)).to be nil end end end context '件数取得' do - before do - sign_in @user_yas - end - it 'ステータス OKを返す' do - return_ok_at_get :count_by_author, id: @author_yas.id, format: :json - end context 'html形式' do it '必要ないので実装していない' do - get :count_by_author, id: @author_yas.id + sign_in @user_yas + get :count_by_author, @query expect(response.status).to eq(406) end end context 'json形式' do it 'jsonデータを返す' do - get :count_by_author, id: @author_yas.id, format: :json - expect {JSON.parse(response.body)}.not_to raise_error + sign_in @user_yas + get :count_by_author, @query_format_json + return_ok? + render_json? + end + end + context 'ゲスト(サインインしていない)' do + it '通常通り応答している' do + get :count_by_author, @query_format_json + return_ok? + render_json? end end end context 'サインインの状態が例外的なとき' do - it 'ユーザではなく管理者でも通常通り応答する' do + it 'ユーザではなく管理者でも通常通り応答している' do sign_in @admin - return_ok_at_get :by_author, id: @author_yas.id - return_ok_at_get :by_author, id: @author_yas.id, format: :json + get :by_author, @query + return_ok? + get :by_author, @query_format_json + render_json? end - it '創作活動してないROM専の読者にも通常通り応答する' do + it '創作活動してないROM専の読者にも通常通り応答している' do sign_in @user_rom - return_ok_at_get :by_author, id: @author_yas.id - return_ok_at_get :by_author, id: @author_yas.id, format: :json + return_ok? + get :by_author, @query_format_json + render_json? end - it 'ゲスト(サインインしていない)はサインインページに遷移する' do - announce_sign_in_at_get :by_author, id: @author_yas.id + it 'ゲスト(サインインしていない)はサインインページに遷移している' do + get :by_author, @query + redirect? sign_in_url + get :by_author, @query_format_json + render_unauthorized? end end end @@ -163,60 +210,68 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 before do @item = FactoryGirl.create :scroll_hello, author: @author_yas @item_id = @item.id + @query = {:id => @item_id} + @query_format_json = @query.merge(:format => :json) + @query_format_prof = @query.merge(:format => :prof) end context '正常系の共通処理' do before do sign_in @user_yas end - it '[html, json, prof]でステータスコード200 OKを返す' do - return_ok_at_get :show, id: @item_id - return_ok_at_get :show, id: @item_id, format: :json - return_ok_at_get :show, id: @item_id, format: :prof - end it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do - get :show, id: @item_id - expect(assigns(:my_model_class)).to be Scroll + get :show, @query + set_model? @my_model_class end it '@itemにリクエストしたアイテムを取得している' do - get :show, id: @item_id + get :show, @query expect(assigns(:item)).to eq(@item) end context 'html形式' do - it 'showテンプレートを描画する' do - get :show, id: @item_id + it 'showテンプレートを描画している' do + get :show, @query + return_ok? expect(response).to render_template("show") end end context 'json形式' do it 'jsonデータを返す' do - get :show, id: @item_id, format: :json - expect {JSON.parse(response.body)}.not_to raise_error + get :show, @query_format_json + return_ok? + render_json? end it 'プロファイラーは必要ないので@profilerに用意していない' do - get :show, id: @item_id, format: :json + get :show, @query_format_json expect(assigns(:profiler)).to be nil end end context 'prof形式' do - it 'プロファイラーテンプレートを描画する' do - get :show, id: @item_id, format: :prof - expect(response).to render_template("templates/r/profiler/profiler") + it 'プロファイラーテンプレートを描画している' do + get :show, @query_format_prof + return_ok? + render_profiler? end end end context 'サインインの状態が例外的なとき' do - it 'ユーザではなく管理者でも通常通り応答する' do + it 'ユーザではなく管理者でも通常通り応答している' do sign_in @admin - return_ok_at_get :show, id: @item_id - return_ok_at_get :show, id: @item_id, format: :json + get :show, @query + return_ok? + get :show, @query_format_json + render_json? end - it '創作活動してないROM専の読者にも通常通り応答する' do + it '創作活動してないROM専の読者にも通常通り応答している' do sign_in @user_rom - return_ok_at_get :show, id: @item_id - return_ok_at_get :show, id: @item_id, format: :json + get :show, @query + return_ok? + get :show, @query_format_json + render_json? end - it 'ゲスト(サインインしていない)はサインインページに遷移する' do - announce_sign_in_at_get :show, id: @item_id + it 'ゲスト(サインインしていない)はサインインページに遷移している' do + get :show, @query + redirect? sign_in_url + get :show, @query_format_json + render_unauthorized? end end end @@ -225,129 +280,136 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 before do @item = FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas @item_id = @item.id + @query = {:id => @item_id} + @query_format_json = @query.merge(:format => :json) end context '正常系の共通処理' do before do sign_in @user_yas end - it '[html, json]でステータス OKを返す' do - return_ok_at_get :play, id: @item_id - return_ok_at_get :play, id: @item_id, format: :json - end it 'コントローラのマニフェストからScrollPanelモデルを@my_model_classに取得している' do # リクエストの対象はスクロールであるが、返されるのはスクコマのリストである。 # くれぐれも間違えないように。 - get :play, id: @item_id - expect(assigns(:my_model_class)).to be ScrollPanel + get :play, @query + set_model? ScrollPanel end it '@itemにリクエストしたスクロールを取得している' do - get :play, id: @item_id + get :play, @query expect(assigns(:item)).to eq(@item) end it 'リストグループのマニフェストからプレイリストの取得処理を@listに取得している' do - get :play, id: @item_id - expect(assigns(:list).item_name).to eq('scroll_panel') - expect(assigns(:list).list_name).to eq('play') + get :play, @query + set_list? 'scroll_panel', 'play' end it '@itemsにプレイリストを取得している' do - get :play, id: @item_id - expect(assigns(:items).size).to be >= 3 - expect(assigns(:count)).to be >= 3 - expect(assigns(:pager)).not_to be nil + get :play, @query + set_play_list? end context 'html形式' do - it 'playテンプレートを描画する' do - get :play, id: @item_id + it 'playテンプレートを描画している' do + get :play, @query + return_ok? expect(response).to render_template("play") end end context 'json形式' do it 'jsonデータを返す' do - get :play, id: @item_id, format: :json - expect {JSON.parse(response.body)}.not_to raise_error + get :play, @query_format_json + return_ok? + render_json? end end end context 'サインインの状態が例外的なとき' do - it 'ユーザではなく管理者でも通常通り応答する' do + it 'ユーザではなく管理者でも通常通り応答している' do sign_in @admin - return_ok_at_get :play, id: @item_id - return_ok_at_get :play, id: @item_id, format: :json + get :play, @query + return_ok? + get :play, @query_format_json + render_json? end - it '創作活動してないROM専の読者にも通常通り応答する' do + it '創作活動してないROM専の読者にも通常通り応答している' do sign_in @user_rom - return_ok_at_get :play, id: @item_id - return_ok_at_get :play, id: @item_id, format: :json + get :play, @query + return_ok? + get :play, @query_format_json + render_json? end - it 'ゲスト(サインインしていない)はサインインページに遷移する' do - announce_sign_in_at_get :play, id: @item_id + it 'ゲスト(サインインしていない)はサインインページに遷移している' do + get :play, @query + redirect? sign_in_url + get :play, @query_format_json + render_unauthorized? end end end describe '新規作成フォーム表示に於いて' do before do + @query = { } + @query_format_json = @query.merge(:format => :json) end context '正常系の共通処理' do before do sign_in @user_yas end - it '[html, json]でステータス OKを返す' do - return_ok_at_get :new - return_ok_at_get :new, format: :json - end it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do - get :new - expect(assigns(:my_model_class)).to be Scroll + get :new, @query + set_model? @my_model_class end it '@itemに新規データを用意している' do - get :new + get :new, @query @item = assigns(:item) - expect(@item.is_a?(Scroll)).to be true + expect(@item.is_a?(@my_model_class)).to be true expect(@item.new_record?).to be true end it '@formに入力フォーム(Bucket)を取得している' do - get :new - @item = assigns(:item) - expect(assigns(:form).item).to eq(@item) - expect(assigns(:form).mounted).to eq(true) - expect(assigns(:form).submit).to eq(true) + get :new, @query + set_bucket? assigns(:item) end it 'スクロールモデルにデフォルト値補充とブーストを依頼している' do - allow_any_instance_of(Scroll).to receive(:supply_default) - allow_any_instance_of(Scroll).to receive(:boosts) - expect_any_instance_of(Scroll).to receive(:supply_default) - expect_any_instance_of(Scroll).to receive(:boosts) - get :new + allow_any_instance_of(@my_model_class).to receive(:supply_default) + allow_any_instance_of(@my_model_class).to receive(:boosts) + expect_any_instance_of(@my_model_class).to receive(:supply_default) + expect_any_instance_of(@my_model_class).to receive(:boosts) + get :new, @query end context 'html形式' do - it 'formテンプレートを描画する' do - get :new - expect(response).to render_template("templates/r/form/form") + it 'formテンプレートを描画している' do + get :new, @query + return_ok? + render_form? end end context 'json形式' do it 'jsonデータを返す' do - get :new, format: :json - expect {JSON.parse(response.body)}.not_to raise_error + get :new, @query_format_json + return_ok? + render_json? end end end context 'サインインの状態が例外的なとき' do before do end - it '創作活動してないROM専の読者には作家登録ページへ遷移する' do + it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do sign_in @user_rom - get :new - announce_regist_author_for_html - announce_regist_author_for_json :get + get :new, @query + redirect? new_author_path + announce_regist_author_for_json :get, @query_format_json, :new end - it 'ゲスト(サインインしていない)はサインインページに遷移する' do - announce_sign_in_at_get :new + it 'ゲスト(サインインしていない)はサインインページに遷移している' do + get :new, @query + redirect? sign_in_url + get :new, @query_format_json + render_unauthorized? end - it 'ユーザではなく管理者でもサインインページに遷移する' do + it 'ユーザではなく管理者でもサインインページに遷移している' do sign_in @admin - announce_sign_in_at_get :new + get :new, @query + redirect? sign_in_url + get :new, @query_format_json + render_unauthorized? end end end @@ -356,86 +418,87 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 before do @attributes = FactoryGirl.attributes_for(:scroll_hello, author_id: @author_yas.id) @attr = {:scroll => @attributes} + @attr_format_json = @attr.merge(:format => :json) end context '正常系の共通処理' do before do sign_in @user_yas end it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do - allow_save Scroll - post_save @attr - expect(assigns(:my_model_class)).to be Scroll + allow_save @my_model_class + post :create, @attr + set_model? @my_model_class end it '@itemに新規アイテムを用意している' do - allow_save Scroll - post_save @attr + allow_save @my_model_class + post :create, @attr @item = assigns(:item) - expect(@item.is_a?(Scroll)).to be true + expect(@item.is_a?(@my_model_class)).to be true + end + it '作成されている' do + expect { + post :create, @attr + }.to change {@my_model_class.count}.by( 1) end context 'html形式' do - it 'ステータスコード302 Foundを返して, 作成されたアイテムの表示ページへ遷移する' do - post_save @attr - expect(response.status).to eq(302) - expect(response).to redirect_to Scroll.last + it '作成されたアイテムの表示ページへ遷移している' do + post :create, @attr + redirect? @my_model_class.last end end context 'json形式' do it 'ステータスコード200 OKを返して, 作成されたアイテムをjsonデータで返す' do - post_save @attr.merge(:format => :json) - expect(response).to be_success - expect {JSON.parse(response.body)}.not_to raise_error + post :create, @attr_format_json + return_ok? + render_json? end end end context '検証、保存に失敗したとき' do before do sign_in @user_yas - reject_save Scroll + reject_save @my_model_class end context 'html形式' do - it 'ステータスコード200 OKを返して, formテンプレートを描画する' do - post_save @attr - expect(response).to be_success - expect(response).to render_template("templates/r/form/form") + it 'ステータスコード200 OKを返して, formテンプレートを描画している' do + post :create, @attr + return_ok? + render_form? end it '@formに入力フォーム(Bucket)を取得している' do - post_save @attr - @item = assigns(:item) - expect(assigns(:form).item).to eq(@item) - expect(assigns(:form).mounted).to eq(true) - expect(assigns(:form).submit).to eq(true) + post :create, @attr + set_bucket? assigns(:item) end end context 'json形式' do - it 'ステータスコード422 unprocessable_entityを返して, 応答メッセージUnprocessable Entityを返す' do - post_save @attr.merge(:format => :json) - expect(response.status).to eq(422) - expect(response.message).to match(/Unprocessable/) + it '処理不能を返している' do + post :create, @attr_format_json + render_unprocessable? end end end context 'サインインの状態が例外的なとき' do before do - allow_save Scroll + allow_save @my_model_class end - it '創作活動してないROM専の読者には作家登録ページへ遷移する' do + it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do sign_in @user_rom - post_save @attr - announce_regist_author_for_html - announce_regist_author_for_json :post, @attr + post :create, @attr + redirect? new_author_path + announce_regist_author_for_json :post, @attr_format_json, :create end - it 'ゲスト(サインインしていない)はサインインページに遷移する' do - post_save @attr - announce_sign_in_for_html - post_save @attr.merge(:format => :json) - announce_sign_in_for_json + it 'ゲスト(サインインしていない)はサインインページに遷移している' do + post :create, @attr + redirect? sign_in_url + post :create, @attr_format_json + render_unauthorized? end - it 'ユーザではなく管理者でもサインインページに遷移する' do + it 'ユーザではなく管理者でもサインインページに遷移している' do sign_in @admin - post_save @attr - announce_sign_in_for_html - post_save @attr.merge(:format => :json) - announce_sign_in_for_json + post :create, @attr + redirect? sign_in_url + post :create, @attr_format_json + render_unauthorized? end end end @@ -444,66 +507,69 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 before do @item = FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas @item_id = @item.id + @query = {:id => @item_id} + @query_format_json = @query.merge(:format => :json) end context '正常系の共通処理' do before do sign_in @user_yas end - it '[html, json]でステータス OKを返す' do - return_ok_at_get :edit, id: @item_id - return_ok_at_get :edit, id: @item_id, format: :json - end it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do - get :edit, id: @item_id - expect(assigns(:my_model_class)).to be Scroll + get :edit, @query + set_model? @my_model_class end - it '@itemにリクエストしたスクロールを取得している' do - get :edit, id: @item_id - expect(assigns(:item).is_a?(Scroll)).to be true + it '@itemにリクエストしたアイテムを取得している' do + get :edit, @query + expect(assigns(:item).is_a?(@my_model_class)).to be true expect(assigns(:item).new_record?).not_to be true expect(assigns(:item)).to eq(@item) end - it '@formに入力フォーム(Bucket)を取得している' do - get :edit, id: @item_id - @item = assigns(:item) - expect(assigns(:form).item).to eq(@item) - expect(assigns(:form).mounted).to eq(true) - expect(assigns(:form).submit).to eq(true) - end it 'スクロールモデルにデフォルト値補充とブーストを依頼している' do - allow_any_instance_of(Scroll).to receive(:boosts) - expect_any_instance_of(Scroll).to receive(:boosts) - get :edit, id: @item_id + allow_any_instance_of(@my_model_class).to receive(:boosts) + expect_any_instance_of(@my_model_class).to receive(:boosts) + get :edit, @query end context 'html形式' do - it 'formテンプレートを描画する' do - get :edit, id: @item_id - expect(response).to render_template("templates/r/form/form") + it 'formテンプレートを描画している' do + get :edit, @query + return_ok? + render_form? + end + it '@formに入力フォーム(Bucket)を取得している' do + get :edit, @query + set_bucket? assigns(:item) end end context 'json形式' do it 'jsonデータを返す' do - get :edit, id: @item_id, format: :json - expect {JSON.parse(response.body)}.not_to raise_error + get :edit, @query_format_json + return_ok? + render_json? end end end context 'サインインの状態が例外的なとき' do before do - allow_save Scroll + allow_save @my_model_class end - it '創作活動してないROM専の読者には作家登録ページへ遷移する' do + it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do sign_in @user_rom - get :edit, id: @item_id - expect(response.status).to eq(302) - expect(response).to redirect_to new_author_path + get :edit, @query + redirect? new_author_path + announce_regist_author_for_json :get, @query_format_json, :edit end - it 'ゲスト(サインインしていない)はサインインページに遷移する' do - announce_sign_in_at_get :new + it 'ゲスト(サインインしていない)はサインインページに遷移している' do + post :edit, @query + redirect? sign_in_url + post :edit, @query_format_json + render_unauthorized? end - it 'ユーザではなく管理者でもサインインページに遷移する' do + it 'ユーザではなく管理者でもサインインページに遷移している' do sign_in @admin - announce_sign_in_at_get :new + post :edit, @query + redirect? sign_in_url + post :edit, @query_format_json + render_unauthorized? end end end @@ -514,34 +580,34 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 @item_id = @item.id @attributes = FactoryGirl.attributes_for(:scroll_hidden, author_id: @author_yas.id) @attr = {:id => @item_id, :scroll => @attributes} + @attr_format_json = @attr.merge(:format => :json) end context '正常系の共通処理' do before do sign_in @user_yas end it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do - allow_save Scroll - put_save @attr - expect(assigns(:my_model_class)).to be Scroll + allow_save @my_model_class + put :update, @attr + set_model? @my_model_class end it '@itemにリクエストしたアイテムを取得して, 更新している' do - allow_save Scroll - put_save @attr - expect(assigns(:item).is_a?(Scroll)).to be true + allow_save @my_model_class + put :update, @attr + expect(assigns(:item).is_a?(@my_model_class)).to be true expect(assigns(:item).visible).to eq(0) expect(assigns(:item)).to eq(@item) end context 'html形式' do - it 'ステータスコード302 Foundを返して, 作成されたアイテムの表示ページへ遷移する' do - put_save @attr - expect(response.status).to eq(302) - expect(response).to redirect_to @item + it '作成されたアイテムの表示ページへ遷移している' do + put :update, @attr + redirect? @item end end context 'json形式' do it 'ステータスコード200 OKを返して, ページ本体は特に返さない' do - put_save @attr.merge(:format => :json) - expect(response).to be_success + put :update, @attr_format_json + return_ok? expect(response.message).to match /./ end end @@ -549,52 +615,48 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 context '検証、保存に失敗したとき' do before do sign_in @user_yas - reject_save Scroll + reject_save @my_model_class end context 'html形式' do - it 'ステータスコード200 OKを返して, formテンプレートを描画する' do - put_save @attr - expect(response).to be_success - expect(response).to render_template("templates/r/form/form") + it 'ステータスコード200 OKを返して, formテンプレートを描画している' do + put :update, @attr + return_ok? + render_form? end it '@formに入力フォーム(Bucket)を取得している' do - put_save @attr - @item = assigns(:item) - expect(assigns(:form).item).to eq(@item) - expect(assigns(:form).mounted).to eq(true) - expect(assigns(:form).submit).to eq(true) + put :update, @attr + set_bucket? assigns(:item) end end context 'json形式' do - it 'ステータスコード422 unprocessable_entityを返して, 応答メッセージUnprocessable Entityを返す' do - put_save @attr.merge(:format => :json) - expect(response.status).to eq(422) - expect(response.message).to match(/Unprocessable/) + it '処理不能を返している' do + put :update, @attr_format_json + render_unprocessable? end end end context 'サインインの状態が例外的なとき' do before do - allow_save Scroll + allow_save @my_model_class end - it '創作活動してないROM専の読者には作家登録ページへ遷移する' do + it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do sign_in @user_rom - put_save @attr - announce_regist_author_for_html - announce_regist_author_for_json :put, @attr + put :update, @attr + redirect? new_author_path + announce_regist_author_for_json :put, @attr_format_json, :update end - it 'ゲスト(サインインしていない)はサインインページに遷移する' do - put_save @attr - announce_sign_in_for_html - put_save @attr.merge(:format => :json) - announce_sign_in_for_json + it 'ゲスト(サインインしていない)はサインインページに遷移している' do + put :update, @attr + redirect? sign_in_url + put :update, @attr_format_json + render_unauthorized? end - it 'ユーザではなく管理者でもサインインページに遷移する' do + it 'ユーザではなく管理者でもサインインページに遷移している' do sign_in @admin - put_save @attr - announce_sign_in_for_html - put_save @attr.merge(:format => :json) - announce_sign_in_for_json + put :update, @attr + redirect? sign_in_url + put :update, @attr_format_json + render_unauthorized? end end end @@ -604,43 +666,42 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 @item = FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas @item_id = @item.id @attr = {:id => @item_id} - @attr_with_json = @attr.merge(:format => :json) + @attr_format_json = @attr.merge(:format => :json) end context '正常系の共通処理' do before do sign_in @user_yas end it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do - allow_destroy Scroll - destroy_save @attr - expect(assigns(:my_model_class)).to be Scroll + allow_destroy @my_model_class + delete :destroy, @attr + set_model? @my_model_class end it '@itemにリクエストしたアイテムを取得している' do - allow_destroy Scroll - destroy_save @attr - expect(assigns(:item).is_a?(Scroll)).to be true + allow_destroy @my_model_class + delete :destroy, @attr + expect(assigns(:item).is_a?(@my_model_class)).to be true expect(assigns(:item)).to eq(@item) end it '削除されている' do - lambda { - destroy_save @attr - }.should change Scroll, :count + expect { + delete :destroy, @attr + }.to change {@my_model_class.count}.by(-1) end it 'ぶら下がるリーフも削除されている' do - lambda { - destroy_save @attr - }.should change ScrollPanel, :count + expect { + delete :destroy, @attr + }.to change {ScrollPanel.count}.from(4).to(0) end context 'html形式' do - it 'ステータスコード302 Foundを返して, マイスクロールの一覧ページへ遷移する' do - destroy_save @attr - expect(response.status).to eq(302) - expect(response).to redirect_to '/home/scrolls' + it 'マイスクロールの一覧ページへ遷移している' do + delete :destroy, @attr + redirect? '/home/scrolls' end end context 'json形式' do it 'ステータスコード200 OKを返して, ページ本体は特に返さない' do - destroy_save @attr_with_json + delete :destroy, @attr_format_json expect(response).to be_success expect(response.message).to match /./ end @@ -649,45 +710,43 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 context '検証、保存に失敗したとき' do before do sign_in @user_yas - reject_destroy Scroll + reject_destroy @my_model_class end context 'html形式' do - it 'ステータスコード302 Foundを返して, そのスクロールの詳細ページへ遷移する' do - destroy_save @attr - expect(response.status).to eq(302) - expect(response).to redirect_to @item + it 'リクエストしたアイテムの詳細ページへ遷移している' do + delete :destroy, @attr + redirect? @item end end context 'json形式' do - it 'ステータスコード422 unprocessable_entityを返して, 応答メッセージUnprocessable Entityを返す' do - destroy_save @attr_with_json - expect(response.status).to eq(422) - expect(response.message).to match(/Unprocessable/) + it '処理不能を返している' do + delete :destroy, @attr_format_json + render_unprocessable? end end end context 'サインインの状態が例外的なとき' do before do - allow_destroy Scroll + allow_destroy @my_model_class end - it '創作活動してないROM専の読者には作家登録ページへ遷移する' do + it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do sign_in @user_rom - destroy_save @attr - announce_regist_author_for_html - announce_regist_author_for_json :delete, @attr_with_json + delete :destroy, @attr + redirect? new_author_path + announce_regist_author_for_json :delete, @attr_format_json, :destroy end - it 'ゲスト(サインインしていない)はサインインページに遷移する' do - destroy_save @attr - announce_sign_in_for_html - destroy_save @attr_with_json - announce_sign_in_for_json + it 'ゲスト(サインインしていない)はサインインページに遷移している' do + delete :destroy, @attr + redirect? sign_in_url + delete :destroy, @attr_format_json + render_unauthorized? end - it 'ユーザではなく管理者でもサインインページに遷移する' do + it 'ユーザではなく管理者でもサインインページに遷移している' do sign_in @admin - destroy_save id: @item_id - announce_sign_in_for_html - destroy_save @attr_with_json - announce_sign_in_for_json + delete :destroy, @attr + redirect? sign_in_url + delete :destroy, @attr_format_json + render_unauthorized? end end end diff --git a/spec/support/controller_macros.rb b/spec/support/controller_macros.rb index 7b8981a9..3df49f63 100644 --- a/spec/support/controller_macros.rb +++ b/spec/support/controller_macros.rb @@ -1,72 +1,84 @@ module ControllerMacros - def list_should_return_filer_or_json action, params = {} - # html形式: - # ステータスコード200 OKを返す - # filerテンプレートを描画する - get action, params + def sign_in_url + '/users/sign_in' + end + + def return_ok? + # ステータス OKを返す expect(response).to be_success - expect(response).to render_template("templates/r/filer/filer") - # json形式 - # ステータスコード200 OKを返す + end + + def render_json? # jsonデータを返す - get action, params.merge(:format => :json) - expect(response).to be_success expect {JSON.parse(response.body)}.not_to raise_error end - # ゲストが画面からコンテンツにアクセスしようとした時の動作を確認する - def announce_sign_in_for_html + def render_filer? + # filerテンプレートを描画する + expect(response).to render_template("templates/r/filer/filer") + end + + def render_profiler? + # profilerテンプレートを描画する + expect(response).to render_template("templates/r/profiler/profiler") + end + + def render_form? + # formテンプレートを描画する + expect(response).to render_template("templates/r/form/form") + end + + def redirect? url # html形式: ステータスコード302 Foundを返す expect(response.status).to eq(302) - expect(response).to redirect_to '/users/sign_in' + expect(response).to redirect_to url end # ゲストがapiからコンテンツにアクセスしようとした時の動作を確認する - def announce_sign_in_for_json + def render_unauthorized? # json形式: ステータスコード401 Unauthorizedを返す expect(response.status).to eq(401) expect(response.message).to match(/Unauthorized/) end - # ゲストがコンテンツにアクセスしようとしたとき、サインインページに遷移する。 - # ほとんどすべての機能から利用されるので、 htmlとjsonをまとめて処理している。 - # 作成と更新はメソッドが違うので、個別にチェックする。 - def announce_sign_in_at_get action, params = {} - get action, params - announce_sign_in_for_html - get action, params.merge(:format => :json) - announce_sign_in_for_json + # 処理不能を返す + def render_unprocessable? + expect(response.status).to eq(422) + expect(response.message).to match(/Unprocessable/) end - # 作家活動をしていないユーザーが画面から創作活動をしようとしたとき、作家登録ページに遷移する - def announce_regist_author_for_html - expect(response.status).to eq(302) - expect(response).to redirect_to new_author_path + def set_model? model_class, var_name = :my_model_class + # @filerにファイラーを取得している + expect(assigns(var_name).class).to be model_class.class end - # 作家活動していないユーザーがAPIから創作活動をしようとしたとき、例外を発生させる。 - # 暫定的な仕様なので、良い方法があれば変えるかもしれない。メンテしやすいように切り分けてある。 - # リクエストして例外が発生したところとらえるので、リクエストと検証を同時に行う - def announce_regist_author_for_json method, params = {}, action = :create - expect {__send__(method, action, params.merge(:format => :json))}.to raise_error + def set_list? item_name, list_name, var_name = :list + expect(assigns(var_name).item_name).to eq(item_name) + expect(assigns(var_name).list_name).to eq(list_name) end - def return_ok_at_get action, params = {} - # ステータスコード200 OKを返す - get action, params - expect(response).to be_success + def set_play_list? + expect(assigns(:items).size).to be >= 3 + expect(assigns(:count)).to be >= 3 + expect(assigns(:pager)).not_to be nil end - def post_save params, action = :create - post action, params + def let_filer? var_name = :filer + expect(assigns(var_name).class).to eq(Locmare::Filer) end - def put_save params, action = :update - put action, params + def set_bucket? item, var_name = :form + # @filerにファイラーを取得している + expect(assigns(var_name).item).to eq(item) + expect(assigns(var_name).mounted).to eq(true) + expect(assigns(var_name).submit).to eq(true) end - def destroy_save params, action = :destroy - delete action, params + # 作家活動していないユーザーがAPIから創作活動をしようとしたとき、例外を発生させる。 + # 暫定的な仕様なので、良い方法があれば変えるかもしれない。メンテしやすいように切り分けてある。 + # リクエストして例外が発生したところとらえるので、リクエストと検証を同時に行う + def announce_regist_author_for_json method, params = {}, action = :create + expect {__send__(method, action, params.merge(:format => :json))}.to raise_error(ActiveRecord::Forbidden) end # 保存処理を成功したことにしてスキップする @@ -89,26 +101,6 @@ module ControllerMacros allow_any_instance_of(my_model_class).to receive(:destroy).and_return(false) end - def show_action_return_profiler item_id - # prof形式: ステータスコード200 OKを返す - get :show, :id => item_id, :format => :prof - expect(response).to be_success - # json形式: ステータスコード200 OKを返す - get action, params.merge(:format => :json) - expect(response).to be_success - end - - def announce_regist_owner_in_at_get action, params = {} - # html形式: ステータスコード302 Foundを返す - get action, params - expect(response.status).to eq(302) - expect(response).to redirect_to '/users/sign_in' - # json形式: ステータスコード401 Unauthorizedを返す - get action, params.merge(:format => :json) - expect(response.status).to eq(401) - expect(response.message).to match(/Unauthorized/) - end - def factory_contents factory_name, author, count = 5 FactoryGirl.create_list(factory_name, count, author: author) end -- 2.11.0