Add test for conference registration controller show and destroy action
After failed delete, user should be redirected to registration show and not root.
This commit is contained in:
parent
80a06b99b4
commit
19157ac81d
3 changed files with 96 additions and 2 deletions
|
|
@ -90,8 +90,8 @@ class ConferenceRegistrationsController < ApplicationController
|
||||||
redirect_to root_path,
|
redirect_to root_path,
|
||||||
notice: "You are not registered for #{@conference.title} anymore!"
|
notice: "You are not registered for #{@conference.title} anymore!"
|
||||||
else
|
else
|
||||||
redirect_to root_path,
|
redirect_to conference_conference_registrations_path(@conference.short_title),
|
||||||
error: "Could not update your registration for #{@conference.title}: "\
|
error: "Could not delete your registration for #{@conference.title}: "\
|
||||||
"#{@registration.errors.full_messages.join('. ')}."
|
"#{@registration.errors.full_messages.join('. ')}."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -106,6 +106,7 @@ Osem::Application.routes.draw do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: change conference_registrations to singular resource
|
||||||
resource :conference_registrations, path: 'register'
|
resource :conference_registrations, path: 'register'
|
||||||
resources :tickets, only: [:index]
|
resources :tickets, only: [:index]
|
||||||
resources :ticket_purchases, only: [:create, :destroy]
|
resources :ticket_purchases, only: [:create, :destroy]
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,54 @@ describe ConferenceRegistrationsController, type: :controller do
|
||||||
context 'user is signed in' do
|
context 'user is signed in' do
|
||||||
before { sign_in(user) }
|
before { sign_in(user) }
|
||||||
|
|
||||||
|
describe 'GET #show' do
|
||||||
|
before do
|
||||||
|
@registration = create(:registration, conference: conference, user: user)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'successful request' do
|
||||||
|
before do
|
||||||
|
get :show, conference_id: conference.short_title
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'assigns conference, registration and workshops variables' do
|
||||||
|
expect(assigns(:conference)).to eq conference
|
||||||
|
expect(assigns(:registration)).to eq @registration
|
||||||
|
expect(assigns(:workshops)).to eq @registration.workshops
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'renders the show template' do
|
||||||
|
expect(response).to render_template('show')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'user has purchased a ticket' do
|
||||||
|
before do
|
||||||
|
@ticket = create(:ticket, conference: conference)
|
||||||
|
@purchased_ticket = create(:ticket_purchase, conference: conference,
|
||||||
|
user: user,
|
||||||
|
ticket: @ticket)
|
||||||
|
get :show, conference_id: conference.short_title
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'assigns price of purchased tickets to total_price and purchased tickets to tickets' do
|
||||||
|
expect(assigns(:total_price)).to eq Money.new(10000, 'USD')
|
||||||
|
expect(assigns(:tickets)).to match_array [@purchased_ticket]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'user has not purchased any ticket' do
|
||||||
|
before do
|
||||||
|
get :show, conference_id: conference.short_title
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'assigns 0 dollars to total_price and empty array to tickets variables' do
|
||||||
|
expect(assigns(:total_price)).to eq Money.new(0, 'USD')
|
||||||
|
expect(assigns(:tickets)).to match_array []
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe 'GET #edit' do
|
describe 'GET #edit' do
|
||||||
before do
|
before do
|
||||||
@registration = create(:registration, conference: conference, user: user)
|
@registration = create(:registration, conference: conference, user: user)
|
||||||
|
|
@ -72,5 +120,50 @@ describe ConferenceRegistrationsController, type: :controller do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'DELETE #destroy' do
|
||||||
|
before do
|
||||||
|
@registration = create(:registration, conference: conference, user: user)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'deletes successfully' do
|
||||||
|
before(:each, run: true) do
|
||||||
|
delete :destroy, conference_id: conference.short_title
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'redirects to root path', run: true do
|
||||||
|
expect(response).to redirect_to root_path
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'shows success message in flash notice', run: true do
|
||||||
|
expect(flash[:notice]).to match('You are not registered for The dog and pony show anymore!')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'deletes the registration' do
|
||||||
|
expect do
|
||||||
|
delete :destroy, conference_id: conference.short_title
|
||||||
|
end.to change{ Registration.count }.from(1).to(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'delete fails' do
|
||||||
|
before do
|
||||||
|
allow_any_instance_of(Registration).to receive(:destroy).and_return(false)
|
||||||
|
delete :destroy, 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 error in flash message' do
|
||||||
|
expect(flash[:error]).to match "Could not delete your registration for The dog and pony show: #{@registration.errors.full_messages.join('. ')}."
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not delete the registration' do
|
||||||
|
expect(assigns(:registration)).to eq @registration
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue