From 8975df6a6b68a434208c30364e96ce3d4be7f2aa Mon Sep 17 00:00:00 2001 From: Ana Date: Tue, 9 Aug 2016 16:23:38 +0200 Subject: [PATCH] Event schedules controller tests improved --- .../admin/event_schedules_controller_spec.rb | 99 ++++++++----------- .../admin/schedules_controller_spec.rb | 26 ++--- 2 files changed, 53 insertions(+), 72 deletions(-) diff --git a/spec/controllers/admin/event_schedules_controller_spec.rb b/spec/controllers/admin/event_schedules_controller_spec.rb index 09f06c6c..edac3088 100644 --- a/spec/controllers/admin/event_schedules_controller_spec.rb +++ b/spec/controllers/admin/event_schedules_controller_spec.rb @@ -3,6 +3,7 @@ require 'spec_helper' describe Admin::EventSchedulesController do let(:venue) { create(:venue) } let(:conference) { create(:conference, venue: venue) } + let(:room) { create(:room, venue: venue) } let(:schedule) { create(:schedule, program: conference.program)} let(:event_schedule) { create(:event_schedule, schedule: schedule)} let!(:organizer_role) { Role.find_by(name: 'organizer', resource: conference) } @@ -16,51 +17,42 @@ describe Admin::EventSchedulesController do describe 'POST #create' do context 'with valid attributes' do - it 'saves the event schedule to the database' do - expected = expect do - post :create, conference_id: conference.short_title, event_schedule: - attributes_for(:event_schedule, - schedule_id: schedule.id, - event_id: create(:event, program: conference.program).id, - room_id: create(:room, venue: venue).id, - start_time: conference.start_date) - end - expected.to change { EventSchedule.count }.by 1 - end - - it 'renders JSON without errors' do + let(:create_action) do post :create, conference_id: conference.short_title, event_schedule: attributes_for(:event_schedule, schedule_id: schedule.id, event_id: create(:event, program: conference.program).id, room_id: create(:room, venue: venue).id, start_time: conference.start_date) + end + it 'saves the event schedule to the database' do + expect{ create_action }.to change { EventSchedule.count }.by 1 + end + + it 'renders JSON without errors' do + create_action expect(response).to be_success end end context 'with invalid attributes' do - it 'does not save the event schedule to the database' do - expected = expect do - post :create, conference_id: conference.short_title, event_schedule: - attributes_for(:event_schedule, - schedule_id: schedule.id, - event_id: nil, - room_id: nil, - start_time: nil) - end - expected.to_not change { EventSchedule.count } - end - it 'renders JSON with error' do + let(:create_action) do post :create, conference_id: conference.short_title, event_schedule: attributes_for(:event_schedule, schedule_id: schedule.id, event_id: nil, room_id: nil, start_time: nil) + end + it 'does not save the event schedule to the database' do + expect{ create_action }.to_not change { EventSchedule.count } + end + + it 'renders JSON with error' do + create_action expect(response.status).to eq(422) end end @@ -68,71 +60,60 @@ describe Admin::EventSchedulesController do describe 'POST #update' do context 'with valid attributes' do - it 'changes event schedule attributes' do - event = create(:event, program: conference.program) - room = create(:room, venue: venue) + before :each do patch :update, id: event_schedule.id, conference_id: conference.short_title, event_schedule: attributes_for(:event_schedule, schedule_id: schedule.id, - event_id: event.id, + event_id: create(:event, program: conference.program).id, room_id: room.id, start_time: conference.start_date) event_schedule.reload - expect(event_schedule.schedule_id).to eq(schedule.id) - expect(event_schedule.event_id).to eq(event.id) + end + + it 'updates the room' do expect(event_schedule.room_id).to eq(room.id) + end + + it 'updates the start_time' do expect(event_schedule.start_time).to eq(conference.start_date) end it 'renders JSON without errors' do - patch :update, id: event_schedule.id, conference_id: conference.short_title, event_schedule: - attributes_for(:event_schedule, - schedule_id: schedule.id, - event_id: create(:event, program: conference.program).id, - room_id: create(:room, venue: venue).id, - start_time: conference.start_date) - expect(response).to be_success end end context 'with invalid attributes' do - it 'does not save the event schedule to the database' do - expected = expect do - patch :update, id: event_schedule.id, conference_id: conference.short_title, event_schedule: - attributes_for(:event_schedule, - schedule_id: schedule.id, - event_id: nil, - room_id: nil, - start_time: nil) - end - expected.to_not change { event_schedule } - end - - it 'renders JSON with error' do + let(:update_action) do patch :update, id: event_schedule.id, conference_id: conference.short_title, event_schedule: attributes_for(:event_schedule, schedule_id: schedule.id, event_id: nil, room_id: nil, start_time: nil) + end + it 'does not save the event schedule to the database' do + expect{ update_action }.to_not change { event_schedule } + end + it 'renders JSON with error' do + update_action expect(response.status).to eq(422) end end end describe 'DELETE #destroy' do - it 'deletes the event schedule' do - expected = expect do - delete :destroy, id: event_schedule.id, conference_id: conference.short_title - end - - expected.to change { EventSchedule.count }.by(-1) - end - it 'renders JSON without errors' do + let(:destroy_action) do delete :destroy, id: event_schedule.id, conference_id: conference.short_title + end + it 'deletes the event schedule' do + expect{ destroy_action }.to change { EventSchedule.count }.by(-1) + end + + it 'renders JSON without errors' do + destroy_action expect(response).to be_success end end diff --git a/spec/controllers/admin/schedules_controller_spec.rb b/spec/controllers/admin/schedules_controller_spec.rb index 143ba042..7ba5860e 100644 --- a/spec/controllers/admin/schedules_controller_spec.rb +++ b/spec/controllers/admin/schedules_controller_spec.rb @@ -20,42 +20,42 @@ describe Admin::SchedulesController do end describe 'POST #create' do + let(:create_action){ post :create, conference_id: conference.short_title } + it 'saves the schedule to the database' do - expected = expect do - post :create, conference_id: conference.short_title - end - expected.to change { Schedule.count }.by 1 + expect{ create_action }.to change { Schedule.count }.by 1 end it 'redirects to schedules#show' do - post :create, conference_id: conference.short_title - + create_action expect(response).to redirect_to admin_conference_schedule_path( conference.short_title, assigns[:schedule]) end end describe 'GET #show' do + let(:show_action){ get :show, id: schedule.id, conference_id: conference.short_title } + it 'assigns the requested schedule to schedule' do - get :show, id: schedule.id, conference_id: conference.short_title + show_action expect(assigns(:schedule)).to eq schedule end it 'renders the show template' do - get :show, id: schedule.id, conference_id: conference.short_title + show_action expect(response).to render_template :show end end describe 'DELETE #destroy' do + let(:destroy_action){ delete :destroy, id: schedule.id, conference_id: conference.short_title } + it 'deletes the schedule' do - expected = expect do - delete :destroy, id: schedule.id, conference_id: conference.short_title - end - expected.to change { Schedule.count }.by(-1) + expect{ destroy_action }.to change { Schedule.count }.by(-1) end + it 'redirects to schedules#index' do - delete :destroy, id: schedule.id, conference_id: conference.short_title + destroy_action expect(response).to redirect_to admin_conference_schedules_path(conference.short_title) end end