Skip to content

Commit ed0df77

Browse files
authored
Merge pull request #6171 from NHSDigital/fix-flaky-test
Avoid creating vaccines in tests
2 parents d534ab8 + 76bf0e5 commit ed0df77

8 files changed

Lines changed: 38 additions & 22 deletions

File tree

spec/factories/vaccines.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,13 @@
189189
snomed_product_term { data["snomed_product_term"] }
190190
end
191191
end
192+
193+
before(:create) do |_vaccine, _evaluator|
194+
unless Vaccine.enable_factorybot_factories
195+
raise "Do not create vaccines in tests. " \
196+
"This introduces dependencies between tests as they get cached " \
197+
"on the programme and then deleted from the database."
198+
end
199+
end
192200
end
193201
end

spec/helpers/consents_helper_spec.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@
44
subject(:reasons) { helper.consent_refusal_reasons(consent) }
55

66
shared_examples "refusal reason label" do |expected_label|
7-
before do
8-
create(
9-
:vaccine,
10-
:contains_gelatine,
11-
programme_type: programme.type,
12-
disease_types: programme.disease_types
13-
)
14-
end
15-
167
it "uses the programme-specific refusal reason label" do
178
reason = reasons.find { |reason| reason.value == "contains_gelatine" }
189
expect(reason.label).to eq(expected_label) if reason

spec/lib/fhir_mapper/vaccine_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
subject(:fhir_mapper) { described_class.new(vaccine) }
55

66
let(:vaccine) do
7-
create(
7+
build(
88
:vaccine,
99
:injection,
1010
snomed_product_code: "183817183",
@@ -58,7 +58,7 @@
5858
end
5959

6060
context "with flu injection vaccine, dose 1" do
61-
let(:vaccine) { create(:vaccine, :flu, :injection) }
61+
let(:vaccine) { build(:vaccine, :flu, :injection) }
6262

6363
it "pairs the correct code with the correct term" do
6464
coding =
@@ -72,7 +72,7 @@
7272
end
7373

7474
context "with flu nasal vaccine, dose 2" do
75-
let(:vaccine) { create(:vaccine, :flu, :nasal) }
75+
let(:vaccine) { build(:vaccine, :flu, :nasal) }
7676

7777
it "pairs the correct code with the correct term" do
7878
coding =

spec/models/batch_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
# fk_rails_... (vaccine_id => vaccines.id)
2525
#
2626
describe Batch do
27-
subject(:batch) { build(:batch) }
27+
subject(:batch) { build(:batch, vaccine:) }
28+
29+
let(:vaccine) { Vaccine.all.sample }
2830

2931
describe "scopes" do
30-
let(:archived_batch) { create(:batch, :archived) }
31-
let(:not_archived_batch) { create(:batch) }
32+
let(:archived_batch) { create(:batch, :archived, vaccine:) }
33+
let(:not_archived_batch) { create(:batch, vaccine:) }
3234

3335
describe "#archived" do
3436
subject(:scope) { described_class.archived }
@@ -79,7 +81,7 @@
7981
end
8082

8183
context "when already archived" do
82-
let(:batch) { create(:batch, :archived) }
84+
let(:batch) { create(:batch, :archived, vaccine:) }
8385

8486
it "doesn't change archived_at" do
8587
expect { archive! }.not_to change(batch, :archived_at)

spec/models/health_question_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
let(:vaccine) { create(:vaccine) }
3434
let!(:hqs) { create_list(:health_question, 3, vaccine:) }
3535

36+
around(:all) do |example|
37+
# This is safe because we don't reference a programme at any point.
38+
Vaccine.enable_factorybot_factories = true
39+
example.run
40+
Vaccine.enable_factorybot_factories = false
41+
end
42+
3643
describe "#first_health_question" do
3744
it "returns the first health question" do
3845
hqs.first.update! next_question: hqs.second

spec/policies/batch_policy_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
let(:team) { create(:team) }
88
let(:user) { create(:user, team:) }
99

10-
let(:batch) { create(:batch, team:) }
11-
let(:archived_batch) { create(:batch, :archived, team:) }
12-
let(:non_team_batch) { create(:batch) }
10+
let(:vaccine) { Vaccine.all.sample }
11+
12+
let(:batch) { create(:batch, team:, vaccine:) }
13+
let(:archived_batch) { create(:batch, :archived, team:, vaccine:) }
14+
let(:non_team_batch) { create(:batch, vaccine:) }
1315

1416
it { should include(batch) }
1517
it { should include(archived_batch) }

spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@
220220

221221
config.before(:suite) do
222222
DatabaseCleaner.clean_with(:truncation)
223-
ProgrammeHelper.create_vaccines!
223+
Vaccine.factorybot_create_all!
224224
DatabaseCleaner.strategy = :transaction
225225
end
226226

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# frozen_string_literal: true
22

3-
class ProgrammeHelper
4-
def self.create_vaccines!
3+
class Vaccine
4+
cattr_accessor :enable_factorybot_factories
5+
6+
def self.factorybot_create_all!
7+
Vaccine.enable_factorybot_factories = true
8+
59
FactoryBot.create(:vaccine, :cervarix, programme: Programme.hpv)
610
FactoryBot.create(:vaccine, :gardasil, programme: Programme.hpv)
711
FactoryBot.create(:vaccine, :gardasil_9, programme: Programme.hpv)
@@ -22,5 +26,7 @@ def self.create_vaccines!
2226
FactoryBot.create(:vaccine, :priorix_tetra, programme: Programme.mmr)
2327

2428
FactoryBot.create(:vaccine, :revaxis, programme: Programme.td_ipv)
29+
30+
Vaccine.enable_factorybot_factories = false
2531
end
2632
end

0 commit comments

Comments
 (0)