RailsApp Rails RSpec 2018年 11月 28日
# テストの最初に追加する.自分が所有しないクラスを let で遅延取得できるようにしておく.
let(:not_mine) { kurasu_factory :taro3300 }
(中略)
describe 'GET #edit' do
context 'owned object' do
subject { -> { get edit_kurasu_path(object) } }
it_behaves_like :response_status_check, 200
it_behaves_like :response_body_includes, '2300'
end
context 'not owned object' do
subject { -> { get edit_kurasu_path(not_mine) } }
it_behaves_like :response_status_check, 302
it_behaves_like :redirect_to
it_behaves_like :flash_alert_message, 'クラスを編集する権限がありません.'
end
end
# 先頭に追加
before_action :get_one, only: %i[edit update destroy show]
(中略)
def edit
redirect_to(kurasus_path(edit_mode: true), alert: alert_message(Kurasu)) and return if @kurasu.nil?
end
(中略)
def get_one
@kurasu = current_teacher.kurasus.find_by_id(params[:id])
end
private :get_one
cp app/views/kurasus/{new,edit}.html.haml
describe 'PATCH #update' do
before { @attrs = object.attributes }
context 'owned object' do
subject { -> { patch kurasu_path(object), params: {kurasu: @attrs} } }
context '正しいパラメータに対して' do
before { @attrs['name'] = 'new name' }
it_behaves_like :response_status_check, 302
it_behaves_like :change_object_value_by_update, Kurasu, :name, 'new name'
it_behaves_like :redirect_to
it_behaves_like :flash_notice_message, 'クラスを更新しました.'
end
context '不正なパラメータに対して' do
before { @attrs['name'] = nil }
it_behaves_like :response_status_check, 200
it_behaves_like :not_change_object_value_by_update, Kurasu, :name
it_behaves_like :flash_alert_message, 'クラスが更新できません.'
end
end
context 'not owned object' do
subject { -> { put kurasu_path(not_mine), params: {kurasu: @attrs} } }
it_behaves_like :response_status_check, 302
it_behaves_like :redirect_to
it_behaves_like :flash_alert_message, 'クラスが更新できません.'
end
end
def update
redirect_to(kurasus_path, alert: alert_message(Kurasu)) and return if @kurasu.nil?
if @kurasu.update_attributes(kurasu_params)
redirect_to kurasus_path(edit_mode: true), notice: notice_message(Kurasu)
else
flash.now[:alert] = alert_message(Kurasu)
render action: :edit
end
end
GET #edit
owned object
behaves like response_status_check
response should be 200
behaves like response_body_includes
response body includes 2300
not owned object
behaves like response_status_check
response should be 302
behaves like redirect_to
should redirect to "/kurasus"
behaves like flash_alert_message
should eq "クラスを編集する権限がありません."
PATCH #update
owned object
正しいパラメータに対して
behaves like response_status_check
response should be 302
behaves like change_object_value_by_update
should change `klass.find(object.id).send(key)` from "2300" to "new name"
behaves like redirect_to
should redirect to "/kurasus?edit_mode=true"
behaves like flash_notice_message
should eq "クラスを更新しました."
不正なパラメータに対して
behaves like response_status_check
response should be 200
behaves like not_change_object_value_by_update
should not change `klass.find(object.id).send(key)`
behaves like flash_alert_message
should eq "クラスが更新できません."
not owned object
behaves like response_status_check
response should be 302
behaves like redirect_to
should redirect to "/kurasus?edit_mode=true"
behaves like flash_alert_message
should eq "クラスが更新できません."
長くなったので今日はここまで