From b41d2bd5c166616d085fe29e62c1df8fe3961436 Mon Sep 17 00:00:00 2001 From: Aditya Prakash Date: Tue, 22 Mar 2016 17:13:15 +0530 Subject: [PATCH] Add test for conference registration controller edit and update action --- ...conference_registration_controller_spec.rb | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 spec/controllers/conference_registration_controller_spec.rb diff --git a/spec/controllers/conference_registration_controller_spec.rb b/spec/controllers/conference_registration_controller_spec.rb new file mode 100644 index 00000000..4b571cca --- /dev/null +++ b/spec/controllers/conference_registration_controller_spec.rb @@ -0,0 +1,76 @@ +require 'spec_helper' + +describe ConferenceRegistrationsController, type: :controller do + let(:conference) { create(:conference) } + let(:user) { create(:user) } + + context 'user is signed in' do + before { sign_in(user) } + + describe 'GET #edit' do + before do + @registration = create(:registration, conference: conference, user: user) + get :edit, conference_id: conference.short_title + end + + it 'assigns conference and registration variable' do + expect(assigns(:conference)).to eq conference + expect(assigns(:registration)).to eq @registration + end + + it 'renders the edit template' do + expect(response).to render_template('edit') + end + end + + describe 'PATCH #update' do + before do + @registration = create(:registration, + conference: conference, + user: user, + arrival: Date.new(2014, 04, 25)) + end + + context 'updates successfully' do + before do + patch :update, registration: attributes_for(:registration, arrival: Date.new(2014, 04, 29)), + conference_id: conference.short_title + end + + it 'redirects to registration show path' do + expect(response).to redirect_to conference_conference_registrations_path(conference.short_title) + end + + it 'shows success message in flash notice' do + expect(flash[:notice]).to match('Registration was successfully updated.') + end + + it 'updates the registration' do + @registration.reload + expect(@registration.arrival).to eq Date.new(2014, 04, 29) + end + end + + context 'update fails' do + before do + allow_any_instance_of(Registration).to receive(:update_attributes).and_return(false) + patch :update, registration: attributes_for(:registration, arrival: Date.new(2014, 04, 27)), + conference_id: conference.short_title + end + + it 'renders edit template' do + expect(response).to render_template('edit') + end + + it 'shows error in flash message' do + expect(flash[:error]).to match "Could not update your registration for The dog and pony show: #{@registration.errors.full_messages.join('. ')}." + end + + it 'does not update the registration' do + @registration.reload + expect(@registration.arrival).to eq Date.new(2014, 04, 25) + end + end + end + end +end