From e48e0652918d8086b686b3527bde3ab29ce595f6 Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Wed, 4 Mar 2026 16:49:58 -0700 Subject: [PATCH 1/6] Check for what users see when creating volunteers It avoids hitting the Database when running system tests --- spec/system/volunteers/new_spec.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/spec/system/volunteers/new_spec.rb b/spec/system/volunteers/new_spec.rb index 02e754ecd3..66c2a0d2dd 100644 --- a/spec/system/volunteers/new_spec.rb +++ b/spec/system/volunteers/new_spec.rb @@ -25,7 +25,7 @@ context "when supervisor" do let(:supervisor) { create(:supervisor) } - it "lets Supervisor create new volunteer" do + it "creates a new volunteer", :js do sign_in supervisor visit new_volunteer_path @@ -33,9 +33,12 @@ fill_in "Display name", with: "New Volunteer Display Name 2" fill_in "Date of birth", with: Date.new(2000, 1, 2) - expect do - click_on "Create Volunteer" - end.to change(User, :count).by(1) + click_on "Create Volunteer" + + visit volunteers_path + expect(page).to have_text("New Volunteer Display Name 2") + expect(page).to have_text("new_volunteer2@example.com") + expect(page).to have_text("Active") end end From 13212c3435e66205012016b0dcec9578c8e8d6ef Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Wed, 4 Mar 2026 16:51:57 -0700 Subject: [PATCH 2/6] Reorganize volunteer context Keep volunteer tests in the same context, and have each test creating its own required setup. --- spec/system/volunteers/new_spec.rb | 39 +++++++++++++++--------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/spec/system/volunteers/new_spec.rb b/spec/system/volunteers/new_spec.rb index 66c2a0d2dd..19b68878eb 100644 --- a/spec/system/volunteers/new_spec.rb +++ b/spec/system/volunteers/new_spec.rb @@ -43,32 +43,31 @@ end context "volunteer user" do - let(:volunteer) { create(:volunteer) } - - before { sign_in volunteer } - it "redirects the user with an error message" do + volunteer = create(:volunteer) + sign_in volunteer + visit new_volunteer_path expect(page).to have_selector(".alert", text: "Sorry, you are not authorized to perform this action.") end - end - - it "displays learning hour topic for volunteers when enabled", :js do - organization = create(:casa_org, learning_topic_active: true) - volunteer = create(:volunteer, casa_org: organization) - sign_in volunteer - visit new_learning_hour_path - expect(page).to have_text("Learning Topic") - end - - it "learning hour topic hidden when disabled", :js do - organization = create(:casa_org) - volunteer = create(:volunteer, casa_org: organization) + it "displays learning hour topic when enabled", :js do + organization = create(:casa_org, learning_topic_active: true) + volunteer = create(:volunteer, casa_org: organization) + + sign_in volunteer + visit new_learning_hour_path + expect(page).to have_text("Learning Topic") + end - sign_in volunteer - visit new_learning_hour_path - expect(page).not_to have_text("Learning Topic") + it "does not display learning hour topic when disabled", :js do + organization = create(:casa_org) + volunteer = create(:volunteer, casa_org: organization) + + sign_in volunteer + visit new_learning_hour_path + expect(page).not_to have_text("Learning Topic") + end end end From c42192ec56eca3a87b6e3facae283c41a6aa32ec Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Wed, 4 Mar 2026 17:07:51 -0700 Subject: [PATCH 3/6] Refactor volunteer creation email invitation system test The system test was not checking for anything on the page. This test is better placed at the model level, since it's related to business logic. --- spec/models/volunteer_spec.rb | 15 +++++++++++++++ spec/system/volunteers/new_spec.rb | 21 --------------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/spec/models/volunteer_spec.rb b/spec/models/volunteer_spec.rb index 4cbfced8d6..b2813a08b4 100644 --- a/spec/models/volunteer_spec.rb +++ b/spec/models/volunteer_spec.rb @@ -394,6 +394,21 @@ end end + describe 'invitation' do + it 'delivers an email invite' do + volunteer = build(:volunteer, email: "new_volunteer@example.com") + volunteer.invite! + + email = ActionMailer::Base.deliveries.last + + expect(email).not_to be_nil + expect(email.to).to eq ["new_volunteer@example.com"] + expect(email.subject).to eq("CASA Console invitation instructions") + expect(email.html_part.body.encoded).to match(/your new Volunteer account/i) + expect(Volunteer.find_by(email: "new_volunteer@example.com").invitation_created_at).to be_present + end + end + describe "#learning_hours_spent_in_one_year" do let(:volunteer) { create :volunteer } let(:learning_hour_type) { create :learning_hour_type } diff --git a/spec/system/volunteers/new_spec.rb b/spec/system/volunteers/new_spec.rb index 19b68878eb..545a10d717 100644 --- a/spec/system/volunteers/new_spec.rb +++ b/spec/system/volunteers/new_spec.rb @@ -1,27 +1,6 @@ require "rails_helper" RSpec.describe "volunteers/new", type: :system do - context "when admin" do - let(:admin) { create(:casa_admin) } - - it "creates a new volunteer and sends invitation" do - sign_in admin - visit new_volunteer_path - - fill_in "Email", with: "new_volunteer@example.com" - fill_in "Display name", with: "New Volunteer Display Name" - fill_in "Date of birth", with: Date.new(2001, 8, 8) - - click_on "Create Volunteer" - - last_email = ActionMailer::Base.deliveries.last - expect(last_email.to).to eq ["new_volunteer@example.com"] - expect(last_email.subject).to have_text "CASA Console invitation instructions" - expect(last_email.html_part.body.encoded).to have_text "your new Volunteer account." - expect(Volunteer.find_by(email: "new_volunteer@example.com").invitation_created_at).not_to be_nil - end - end - context "when supervisor" do let(:supervisor) { create(:supervisor) } From 333d570ecfb52352175b54215f2e127d788b95ef Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Wed, 4 Mar 2026 17:12:46 -0700 Subject: [PATCH 4/6] Avoid creating factories when possible Since we're not checking for anything related to an organization, we can speed this test by building factories instead of creating them. --- spec/system/volunteers/new_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/system/volunteers/new_spec.rb b/spec/system/volunteers/new_spec.rb index 545a10d717..5fdc77e90d 100644 --- a/spec/system/volunteers/new_spec.rb +++ b/spec/system/volunteers/new_spec.rb @@ -32,7 +32,7 @@ end it "displays learning hour topic when enabled", :js do - organization = create(:casa_org, learning_topic_active: true) + organization = build(:casa_org, learning_topic_active: true) volunteer = create(:volunteer, casa_org: organization) sign_in volunteer @@ -41,7 +41,7 @@ end it "does not display learning hour topic when disabled", :js do - organization = create(:casa_org) + organization = build(:casa_org) volunteer = create(:volunteer, casa_org: organization) sign_in volunteer From 4824ecec399e108d20ad0e12f9fb5b24e56d16bd Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Wed, 4 Mar 2026 17:21:02 -0700 Subject: [PATCH 5/6] linter --- spec/models/volunteer_spec.rb | 4 ++-- spec/system/volunteers/new_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/models/volunteer_spec.rb b/spec/models/volunteer_spec.rb index b2813a08b4..f1167b6521 100644 --- a/spec/models/volunteer_spec.rb +++ b/spec/models/volunteer_spec.rb @@ -394,8 +394,8 @@ end end - describe 'invitation' do - it 'delivers an email invite' do + describe "invitation" do + it "delivers an email invite" do volunteer = build(:volunteer, email: "new_volunteer@example.com") volunteer.invite! diff --git a/spec/system/volunteers/new_spec.rb b/spec/system/volunteers/new_spec.rb index 5fdc77e90d..3f85677a43 100644 --- a/spec/system/volunteers/new_spec.rb +++ b/spec/system/volunteers/new_spec.rb @@ -34,7 +34,7 @@ it "displays learning hour topic when enabled", :js do organization = build(:casa_org, learning_topic_active: true) volunteer = create(:volunteer, casa_org: organization) - + sign_in volunteer visit new_learning_hour_path expect(page).to have_text("Learning Topic") @@ -43,7 +43,7 @@ it "does not display learning hour topic when disabled", :js do organization = build(:casa_org) volunteer = create(:volunteer, casa_org: organization) - + sign_in volunteer visit new_learning_hour_path expect(page).not_to have_text("Learning Topic") From 58b73cc96679da7f9b08976d55416bc4d1edc6fe Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Wed, 4 Mar 2026 17:22:01 -0700 Subject: [PATCH 6/6] less code to check for the same thing --- spec/models/volunteer_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/volunteer_spec.rb b/spec/models/volunteer_spec.rb index f1167b6521..bc85919e45 100644 --- a/spec/models/volunteer_spec.rb +++ b/spec/models/volunteer_spec.rb @@ -405,7 +405,7 @@ expect(email.to).to eq ["new_volunteer@example.com"] expect(email.subject).to eq("CASA Console invitation instructions") expect(email.html_part.body.encoded).to match(/your new Volunteer account/i) - expect(Volunteer.find_by(email: "new_volunteer@example.com").invitation_created_at).to be_present + expect(volunteer.reload.invitation_created_at).to be_present end end