Skip to content

Commit

Permalink
Use build in specs where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
pollygee committed Sep 11, 2021
1 parent 546468f commit 755ad2e
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 29 deletions.
7 changes: 4 additions & 3 deletions spec/controllers/case_contacts_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
require "rails_helper"

RSpec.describe CaseContactsController, type: :controller do
let(:organization) { create(:casa_org) }
let(:organization) { build(:casa_org) }
let(:volunteer) { create(:volunteer, :with_casa_cases, casa_org: organization) }
let(:admin) { create(:casa_admin) }
let(:supervisor) { create(:supervisor) }
let(:case_id) { volunteer.casa_cases.first.id }
let(:params) { {case_contact: {casa_case_id: case_id}} }
let!(:contact_type_group_one) do
create(:contact_type_group, casa_org: organization).tap do |group|
create(:contact_type, contact_type_group: group, name: "Attorney")
Expand All @@ -32,14 +33,14 @@
end

it "only assigns that contact types groups to @current_organization_groups" do
get :new, params: {case_contact: {casa_case_id: case_id}}
get :new, params: params
expect(assigns(:current_organization_groups)).to eq([contact_type_group_one])
end
end

context "when the case does not have specific contact types assigned" do
it "assigns all the organizations contact type groups to @current_organization_groups" do
get :new, params: {case_contact: {casa_case_id: case_id}}
get :new, params: params
expect(assigns(:current_organization_groups)).to eq([contact_type_group_one, contact_type_group_two])
end

Expand Down
7 changes: 3 additions & 4 deletions spec/controllers/emancipations_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require "rails_helper"

RSpec.describe EmancipationsController, type: :controller do
let(:organization) { create(:casa_org) }
let(:organization) { build(:casa_org) }
let(:volunteer) { create(:volunteer, :with_casa_cases, casa_org: organization) }
let(:test_case_category) { create(:casa_case_emancipation_category) }
let(:test_case_category) { build(:casa_case_emancipation_category) }
let(:casa_case) { create(:casa_case, casa_org: organization) }
subject { post :save, params: params }

before do
Expand Down Expand Up @@ -40,7 +41,6 @@

describe "check_item_action" do
it "raises missing param error message" do
casa_case = create :casa_case, casa_org: organization
post :save, params: {casa_case_id: casa_case.id.to_s}
expect(response.body).to eq({error: "Sorry, you are not authorized to perform this action. Did the session expire?"}.to_json)
end
Expand All @@ -52,7 +52,6 @@
end

it "raises must be positive integer error message" do
casa_case = create :casa_case, casa_org: organization
post :save, params: {casa_case_id: casa_case.id.to_s, check_item_action: "1", check_item_id: "-1"}
expect(response.body).to eq({error: "Sorry, you are not authorized to perform this action. Did the session expire?"}.to_json)
end
Expand Down
12 changes: 6 additions & 6 deletions spec/datatables/volunteer_datatable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.start

org = create :casa_org
org = build(:casa_org)
supervisors = create_list :supervisor, 3, casa_org: org

supervisors.each do |supervisor|
Expand All @@ -42,8 +42,8 @@

volunteers.each_with_index do |volunteer, idx|
volunteer.update display_name: Faker::Name.unique.name, email: Faker::Internet.unique.email
volunteer.casa_cases << create(:casa_case, casa_org: org, transition_aged_youth: false)
volunteer.casa_cases << create(:casa_case, casa_org: org, transition_aged_youth: idx == 1)
volunteer.casa_cases << build(:casa_case, casa_org: org, transition_aged_youth: false)
volunteer.casa_cases << build(:casa_case, casa_org: org, transition_aged_youth: idx == 1)
end
end

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

before do
CasaCase.all.each_with_index { |cc, idx| cc.case_contacts << create(:case_contact, contact_made: true, creator: cc.volunteers.first, occurred_at: idx.days.ago) }
CasaCase.all.each_with_index { |cc, idx| cc.case_contacts << build(:case_contact, contact_made: true, creator: cc.volunteers.first, occurred_at: idx.days.ago) }
end

context "when ascending" do
Expand Down Expand Up @@ -230,11 +230,11 @@

before do
4.times do |i|
create :case_contact, contact_made: true, casa_case: casa_case1, creator: volunteer1, occurred_at: (19 * (i + 1)).days.ago
create(:case_contact, contact_made: true, casa_case: casa_case1, creator: volunteer1, occurred_at: (19 * (i + 1)).days.ago)
end

3.times do |i|
create :case_contact, contact_made: true, casa_case: casa_case2, creator: volunteer2, occurred_at: (29 * (i + 1)).days.ago
create(:case_contact, contact_made: true, casa_case: casa_case2, creator: volunteer2, occurred_at: (29 * (i + 1)).days.ago)
end
end

Expand Down
14 changes: 7 additions & 7 deletions spec/decorators/case_contact_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
context "when the contact_types is an array with three or more values" do
let(:contact_types) do
[
build(:contact_type, name: "School"),
build(:contact_type, name: "Therapist"),
build(:contact_type, name: "Bio Parent")
build_stubbed(:contact_type, name: "School"),
build_stubbed(:contact_type, name: "Therapist"),
build_stubbed(:contact_type, name: "Bio Parent")
]
end

Expand All @@ -68,8 +68,8 @@
context "when the contact types is an array with less than three values" do
let(:contact_types) do
[
build(:contact_type, name: "School"),
build(:contact_type, name: "Therapist")
build_stubbed(:contact_type, name: "School"),
build_stubbed(:contact_type, name: "Therapist")
]
end

Expand Down Expand Up @@ -128,8 +128,8 @@
end

describe "#subheading" do
let(:contact_group) { build(:contact_type_group, name: "Group X") }
let(:contact_type) { build(:contact_type, contact_type_group: contact_group, name: "Type X") }
let(:contact_group) { build_stubbed(:contact_type_group, name: "Group X") }
let(:contact_type) { build_stubbed(:contact_type, contact_type_group: contact_group, name: "Type X") }

context "when all information is available" do
it "returns a properly formatted string" do
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/importers/case_importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
subject(:case_importer) { CaseImporter.new(import_file_path, casa_org_id) }

let(:casa_org_id) { import_user.casa_org.id }
let!(:import_user) { create(:casa_admin) }
let!(:import_user) { build(:casa_admin) }
let(:import_file_path) { Rails.root.join("spec", "fixtures", "casa_cases.csv") }

before do
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/importers/file_importer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "rails_helper"

RSpec.describe FileImporter do
let!(:import_user) { create(:casa_admin) }
let!(:import_user) { build_stubbed(:casa_admin) }
let(:import_file_path) { Rails.root.join("spec", "fixtures", "generic.csv") }
let(:file_importer) { FileImporter.new(import_file_path, import_user.casa_org.id, "something", ["header"]) }

Expand Down
4 changes: 2 additions & 2 deletions spec/lib/importers/supervisor_importer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "rails_helper"

RSpec.describe SupervisorImporter do
let!(:import_user) { build(:casa_admin) }
let!(:import_user) { build_stubbed(:casa_admin) }
let(:casa_org_id) { import_user.casa_org.id }

# Use of the static method SupervisorImporter.import_volunteers functions identically to SupervisorImporter.new(...).import_volunteers
Expand Down Expand Up @@ -39,7 +39,7 @@
end

context "when any volunteer could not be assigned to the supervisor during the import" do
let!(:existing_volunteer) { create(:volunteer, email: "[email protected]") }
let!(:existing_volunteer) { build(:volunteer, email: "[email protected]") }
let(:supervisor_import_data_path) { Rails.root.join("spec", "fixtures", "supervisor_volunteers.csv") }

it "returns an error message" do
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/importers/volunteer_importer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "rails_helper"

RSpec.describe VolunteerImporter do
let!(:import_user) { create(:casa_admin) }
let!(:import_user) { build(:casa_admin) }
let(:casa_org_id) { import_user.casa_org.id }

# Use of the static method VolunteerImporter.import_volunteers functions identically to VolunteerImporter.new(...).import_volunteers
Expand Down
8 changes: 4 additions & 4 deletions spec/mailers/supervisor_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
end

it "does not show a case contact that did not occurr in the week" do
create(:case_contact, casa_case: casa_case, occurred_at: Date.today - 8.days)
build_stubbed(:case_contact, casa_case: casa_case, occurred_at: Date.today - 8.days)
expect(mail.body.encoded).to_not match("Most recent contact attempted:")
end

it "shows the latest case contact that occurred in the week" do
most_recent_contact = create(:case_contact, casa_case: casa_case, occurred_at: Date.today - 1.days, notes: "AAAAAAAAAAAAAAAAAAAAAAAA")
other_contact = create(:case_contact, casa_case: casa_case, occurred_at: Date.today - 3.days, notes: "BBBBBBBBBBBBBBBBBBBB")
other_contact = build(:case_contact, casa_case: casa_case, occurred_at: Date.today - 3.days, notes: "BBBBBBBBBBBBBBBBBBBB")

expect(mail.body.encoded).to match("Notes: #{most_recent_contact.notes}")
expect(mail.body.encoded).to_not match("Notes: #{other_contact.notes}")
Expand All @@ -41,15 +41,15 @@
end

it "does not show a case contact that occurred past the unassignment date in the week" do
contact_past_unassignment = create(:case_contact, casa_case: casa_case, occurred_at: Date.today - 1.days, notes: "AAAAAAAAAAAAAAAAAAAAAAAA")
contact_past_unassignment = build_stubbed(:case_contact, casa_case: casa_case, occurred_at: Date.today - 1.days, notes: "AAAAAAAAAAAAAAAAAAAAAAAA")

expect(mail.body.encoded).to_not match("Notes: #{contact_past_unassignment.notes}")
end

it "shows the latest case contact that occurred in the week before the unassignment date" do
contact_past_unassignment = create(:case_contact, casa_case: casa_case, occurred_at: Date.today - 1.days, notes: "AAAAAAAAAAAAAAAAAAAAAAAA")
most_recent_contact_before_unassignment = create(:case_contact, casa_case: casa_case, occurred_at: Date.today - 3.days, notes: "BBBBBBBBBBBBBBBBBB")
older_contact = create(:case_contact, casa_case: casa_case, occurred_at: Date.today - 4.days, notes: "CCCCCCCCCCCCCCCCCCCC")
older_contact = build_stubbed(:case_contact, casa_case: casa_case, occurred_at: Date.today - 4.days, notes: "CCCCCCCCCCCCCCCCCCCC")

expect(mail.body.encoded).to match("Notes: #{most_recent_contact_before_unassignment.notes}")
expect(mail.body.encoded).to_not match("Notes: #{contact_past_unassignment.notes}")
Expand Down

0 comments on commit 755ad2e

Please sign in to comment.