Skip to content

Commit

Permalink
Refactor testing of flash messages (#7386)
Browse files Browse the repository at this point in the history
Introduce custom matchers have_message and contain_message for checking flash messages.
  • Loading branch information
Raine-Yang-UofT authored Jan 27, 2025
1 parent 18595fa commit dadacb1
Show file tree
Hide file tree
Showing 19 changed files with 141 additions and 138 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- Add unit tests for `marks_graders_controller` (#7382)
- Convert front-end tests from enzyme to react testing library; add `@testing-library/user-event` (#7379)
- Refactor the `Result` component and its children to use React context API (#7380)
- Implement `contain_message` and `have_message` custom Rspec matchers to check for flash message content (#7386)
- Update Python version to 3.13 in seed autotest schemas (#7388)
- Rename jupyter notebook content functions and files to generalize to html content (#7391)

Expand Down
6 changes: 2 additions & 4 deletions spec/controllers/annotation_categories_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,7 @@

expect(response).to have_http_status(:found)
expect(flash[:error]).to be_nil
expect(flash[:success].map { |f| extract_text f }).to eq([I18n.t('upload_success',
count: 2)].map { |f| extract_text f })
expect(flash[:success]).to have_message(I18n.t('upload_success', count: 2))
expect(response).to redirect_to(action: 'index', assignment_id: assignment.id)

expect(AnnotationCategory.all.size).to eq(2)
Expand All @@ -729,8 +728,7 @@

expect(response).to have_http_status :found
expect(flash[:error]).to be_nil
expect(flash[:success].map { |f| extract_text f }).to eq([I18n.t('upload_success',
count: 3)].map { |f| extract_text f })
expect(flash[:success]).to have_message(I18n.t('upload_success', count: 3))
expect(response).to redirect_to(action: 'index', assignment_id: assignment.id)

expect(AnnotationCategory.all.size).to eq 3
Expand Down
12 changes: 5 additions & 7 deletions spec/controllers/assignments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2277,7 +2277,7 @@
grouping # lazy initialization
delete_as instructor, :destroy, params: { course_id: course.id, id: assignment.id }
expect(Assignment.exists?(assignment.id)).to be(true)
expect(flash[:error]).to eq(["<p>#{I18n.t('assignments.assignment_has_groupings')}</p>"])
expect(flash[:error]).to have_message(I18n.t('assignments.assignment_has_groupings'))
expect(flash.to_hash.length).to eq(1)
expect(flash[:error].length).to eq(1)
expect(response).to have_http_status(:found)
Expand All @@ -2286,8 +2286,8 @@
it 'should successfully DELETE assignment (no groups)' do
delete_as instructor, :destroy, params: { course_id: course.id, id: assignment.id }
expect(Assignment.exists?(assignment.id)).to be(false)
expect(flash[:success]).to eq(I18n.t('flash.actions.destroy.success',
resource_name: assignment.short_identifier))
expect(flash[:success]).to have_message(I18n.t('flash.actions.destroy.success',
resource_name: assignment.short_identifier))
expect(flash.to_hash.length).to eq(1)
expect(response).to have_http_status(:found)
end
Expand Down Expand Up @@ -2318,10 +2318,8 @@

delete_as instructor, :destroy, params: { course_id: course.id, id: assignment.id }

expect(flash[:error][0]).to include(
I18n.t('activerecord.errors.models.assignment_deletion',
problem_message: 'some error')
)
expect(flash[:error]).to contain_message(I18n.t('activerecord.errors.models.assignment_deletion',
problem_message: 'some error'))
expect(Assignment.exists?(assignment.id)).to be true
expect(response).to redirect_to(edit_course_assignment_path(course.id, assignment.id))
end
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/automated_tests_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@
end

it 'flashes an error message' do
expect(flash[:error].join('\n')).to include(I18n.t('errors.invalid_path'))
expect(flash[:error]).to contain_message(I18n.t('errors.invalid_path'))
end

it 'does not save the file' do
Expand All @@ -294,7 +294,7 @@
end

it 'flashes an error message' do
expect(flash[:error].join('\n')).to include(I18n.t('errors.invalid_path'))
expect(flash[:error]).to contain_message(I18n.t('errors.invalid_path'))
end

it 'does not create the folder' do
Expand All @@ -309,7 +309,7 @@
end

it 'flashes an error message' do
expect(flash[:error].join('\n')).to include(I18n.t('errors.invalid_path'))
expect(flash[:error]).to contain_message(I18n.t('errors.invalid_path'))
end

it 'does not delete the folder' do
Expand All @@ -324,7 +324,7 @@
end

it 'flashes an error message' do
expect(flash[:error].join('\n')).to include(I18n.t('errors.invalid_path'))
expect(flash[:error]).to contain_message(I18n.t('errors.invalid_path'))
end

it 'does not delete the file' do
Expand Down
3 changes: 1 addition & 2 deletions spec/controllers/courses_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,7 @@
test2 = Assignment.find_by(short_identifier: @test_asn2)
expect(test2).not_to be_nil
expect(flash[:error]).to be_nil
expect(flash[:success].map { |f| extract_text f }).to eq([I18n.t('upload_success',
count: 2)].map { |f| extract_text f })
expect(flash[:success]).to have_message(I18n.t('upload_success', count: 2))
expect(response).to redirect_to(course_assignments_path(course))
end

Expand Down
29 changes: 11 additions & 18 deletions spec/controllers/criteria_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,7 @@
params: { course_id: course.id, id: flexible_criterion.id },
format: :js
expect(assigns(:criterion)).to be_truthy
i18t_strings = [I18n.t('flash.criteria.destroy.success')].map { |f| extract_text f }
expect(i18t_strings).to eql(flash[:success].map { |f| extract_text f })
expect(flash[:success]).to have_message(I18n.t('flash.criteria.destroy.success'))
expect(subject).to respond_with(:success)

expect { FlexibleCriterion.find(flexible_criterion.id) }.to raise_error(ActiveRecord::RecordNotFound)
Expand Down Expand Up @@ -818,8 +817,7 @@
end

it 'displays an error message' do
expect(flash[:error].map { |f| extract_text f })
.to eq([I18n.t('criteria.errors.criteria_not_found')].map { |f| extract_text f })
expect(flash[:error]).to have_message(I18n.t('criteria.errors.criteria_not_found'))
end
end
end
Expand All @@ -832,8 +830,7 @@
params: { course_id: course.id, id: rubric_criterion.id },
format: :js
expect(assigns(:criterion)).to be_truthy
i18t_string = [I18n.t('flash.criteria.destroy.success')].map { |f| extract_text f }
expect(i18t_string).to eql(flash[:success].map { |f| extract_text f })
expect(flash[:success]).to have_message(I18n.t('flash.criteria.destroy.success'))
expect(subject).to respond_with(:success)

expect { RubricCriterion.find(rubric_criterion.id) }.to raise_error(ActiveRecord::RecordNotFound)
Expand Down Expand Up @@ -878,8 +875,7 @@
post_as instructor, :upload,
params: { course_id: course.id, assignment_id: assignment.id, upload_file: empty_file }

expect(flash[:error].map { |f| extract_text f })
.to eq([I18n.t('upload_errors.blank')].map { |f| extract_text f })
expect(flash[:error]).to have_message(I18n.t('upload_errors.blank'))
end

it 'deletes all criteria previously created' do
Expand Down Expand Up @@ -918,8 +914,7 @@
'cr90',
'cr40',
'cr50')
expect(flash[:success].map { |f| extract_text f })
.to eq([I18n.t('upload_success', count: 8)].map { |f| extract_text f })
expect(flash[:success]).to have_message(I18n.t('upload_success', count: 8))
end

it 'creates rubric criteria with properly formatted entries' do
Expand Down Expand Up @@ -1043,8 +1038,7 @@
'cr90',
'cr40',
'cr50')
expect(flash[:success].map { |f| extract_text f })
.to eq([I18n.t('upload_success', count: 8)].map { |f| extract_text f })
expect(flash[:success]).to have_message(I18n.t('upload_success', count: 8))
end

it 'creates criteria correctly when a valid yml file with the wrong extension is uploaded' do
Expand All @@ -1059,17 +1053,16 @@
'cr90',
'cr40',
'cr50')
expect(flash[:success].map { |f| extract_text f })
.to eq([I18n.t('upload_success', count: 8)].map { |f| extract_text f })
expect(flash[:success]).to have_message(I18n.t('upload_success', count: 8))
end

it 'does not create criteria with format errors in entries' do
post_as instructor, :upload, params: { course_id: course.id, assignment_id: assignment.id,
upload_file: invalid_mixed_file }

expect(assignment.criteria.pluck(:name)).not_to include('cr40', 'cr50', 'cr70')
expect(flash[:error].map { |f| extract_text f })
.to eq([I18n.t('criteria.errors.invalid_format') + ' cr40, cr70, cr50'].map { |f| extract_text f })
expect(flash[:error]).to contain_message(I18n.t('criteria.errors.invalid_format'))
expect(flash[:error]).to contain_message(' cr40, cr70, cr50')
end

it 'does not create criteria with an invalid mark' do
Expand All @@ -1084,8 +1077,8 @@
upload_file: missing_levels_file }

expect(assignment.criteria.where(name: %w[no_levels empty_levels])).to be_empty
expect(flash[:error].map { |f| extract_text f })
.to eq([I18n.t('criteria.errors.invalid_format') + ' no_levels, empty_levels'].map { |f| extract_text f })
expect(flash[:error]).to contain_message(I18n.t('criteria.errors.invalid_format'))
expect(flash[:error]).to contain_message(' no_levels, empty_levels')
end

it 'does not create criteria that have both visibility options set to false' do
Expand Down
13 changes: 5 additions & 8 deletions spec/controllers/exam_templates_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
before { post_as user, :create, params: params }

it 'flashes an exam template create failure error message' do
expect(flash[:error].map { |f| extract_text f }).to eq [I18n.t('exam_templates.create.failure')]
expect(flash[:error]).to have_message(I18n.t('exam_templates.create.failure'))
end
end

Expand Down Expand Up @@ -113,7 +113,7 @@
end

it 'displays a flash error message' do
expect(flash[:error].map { |f| extract_text f }).to eq [I18n.t('exam_templates.update.failure')]
expect(flash[:error]).to have_message(I18n.t('exam_templates.update.failure'))
end

it 'responds with 302' do
Expand Down Expand Up @@ -160,8 +160,7 @@
end

it 'should send appropriate error message' do
expect(flash[:error].map { |f| extract_text f })
.to eq([I18n.t('exam_templates.upload_scans.search_failure')].map { |f| extract_text f })
expect(flash[:error]).to have_message(I18n.t('exam_templates.upload_scans.search_failure'))
end
end

Expand All @@ -177,8 +176,7 @@
end

it 'should send appropriate error message' do
expect(flash[:error].map { |f| extract_text f })
.to eq([I18n.t('exam_templates.upload_scans.missing')].map { |f| extract_text f })
expect(flash[:error]).to have_message(I18n.t('exam_templates.upload_scans.missing'))
end
end

Expand All @@ -195,8 +193,7 @@
end

it 'should send appropriate error message' do
expect(flash[:error].map { |f| extract_text f })
.to eq([I18n.t('exam_templates.upload_scans.invalid')].map { |f| extract_text f })
expect(flash[:error]).to have_message(I18n.t('exam_templates.upload_scans.invalid'))
end
end
end
Expand Down
10 changes: 3 additions & 7 deletions spec/controllers/groups_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
end

it 'assigns the error message to flash[:error]' do
expect(flash[:error][0]).to include("Group #{group_name} already exists")
expect(flash[:error]).to contain_message("Group #{group_name} already exists")
end
end
end
Expand Down Expand Up @@ -181,9 +181,7 @@
new_groupname: 'placeholder_group'
}

expect(flash[:error].map do |f|
extract_text f
end).to eq [I18n.t('groups.group_name_already_in_use_diff_assignment')]
expect(flash[:error]).to have_message(I18n.t('groups.group_name_already_in_use_diff_assignment'))
end
end

Expand All @@ -198,9 +196,7 @@
new_groupname: 'placeholder_group'
}

expect(flash[:error].map do |f|
extract_text f
end).to eq [I18n.t('groups.group_name_already_in_use_diff_assignment')]
expect(flash[:error]).to have_message(I18n.t('groups.group_name_already_in_use_diff_assignment'))
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/job_messages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
let(:status_type) { :queued }

it 'should flash a notice message' do
expect(flash[:notice]).to include "<p>#{I18n.t('poll_job.queued')}</p>"
expect(flash[:notice]).to contain_message(I18n.t('poll_job.queued'))
end

it 'should not set the session[:job_id] to nil' do
Expand All @@ -67,7 +67,7 @@
let(:status_type) { :working }

it 'should flash a notice message' do
expect(flash[:notice]).to include "<p>#{ApplicationJob.show_status(status)}</p>"
expect(flash[:notice]).to contain_message(ApplicationJob.show_status(status))
end

it 'should not set the session[:job_id] to nil' do
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/main_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
context 'A non-authenticated user' do
it 'should not be able to login with a blank username' do
post :login, params: { user_login: '', user_password: 'a' }
expect(ActionController::Base.helpers.strip_tags(flash[:error][0])).to eq(I18n.t('main.username_not_blank'))
expect(flash[:error]).to have_message(I18n.t('main.username_not_blank'))
end

it 'should not be able to login with a blank password' do
post :login, params: { user_login: 'a', user_password: '' }
expect(ActionController::Base.helpers.strip_tags(flash[:error][0])).to eq(I18n.t('main.password_not_blank'))
expect(flash[:error]).to have_message(I18n.t('main.password_not_blank'))
end

describe 'login_remote_auth' do
Expand Down Expand Up @@ -143,7 +143,7 @@
context 'after logging in with a bad username' do
it 'should not be able to login with an incorrect username' do
post :login, params: { user_login: instructor.user_name + 'BAD', user_password: 'a' }
expect(ActionController::Base.helpers.strip_tags(flash[:error][0])).to eq(I18n.t('main.login_failed'))
expect(flash[:error]).to have_message(I18n.t('main.login_failed'))
end
end

Expand Down
Loading

0 comments on commit dadacb1

Please sign in to comment.