Skip to content

Commit a3df05f

Browse files
committed
Use selection factory in selection_spec
We've defined the answer_settings in the factory to use Struct rather than OpenStruct for the answer_settings to better mock the behaviour of ActiveResouce::Base which raises a NoMethodError when trying to access attributes that don't exist. Use the factory so we don't need to define this behaviour in multiple places.
1 parent 049b44f commit a3df05f

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

spec/components/question/selection_component/view_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
end
2323

2424
context "when a 'None of the above' question is defined" do
25-
let(:none_of_the_above_question_text_is_optional) { "true" }
25+
let(:none_of_the_above_question_is_optional) { "true" }
2626
let(:question) do
2727
build(:single_selection_question,
2828
:with_none_of_the_above_question,
2929
none_of_the_above_question_text: "Enter another answer",
30-
none_of_the_above_question_text_is_optional:)
30+
none_of_the_above_question_is_optional:)
3131
end
3232

3333
it "renders a conditional text field for the 'None of the above' option" do
@@ -41,7 +41,7 @@
4141
end
4242

4343
context "when the 'None of the above' question is mandatory" do
44-
let(:none_of_the_above_question_text_is_optional) { "false" }
44+
let(:none_of_the_above_question_is_optional) { "false" }
4545

4646
it "has the question text as the label for the field" do
4747
expect(page).to have_css("label[for='form-none-of-the-above-answer-field']", text: "Enter another answer")

spec/factories/models/question/selection.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
trait :with_none_of_the_above_question do
3131
transient do
3232
none_of_the_above_question_text { Faker::Lorem.question }
33-
none_of_the_above_question_text_is_optional { "false" }
33+
none_of_the_above_question_is_optional { "false" }
3434
end
3535
is_optional { true }
3636
none_of_the_above_question do
3737
Struct.new(:question_text, :is_optional)
38-
.new(none_of_the_above_question_text, none_of_the_above_question_text_is_optional)
38+
.new(none_of_the_above_question_text, none_of_the_above_question_is_optional)
3939
end
4040
end
4141

spec/models/question/selection_spec.rb

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
require "rails_helper"
22

33
RSpec.describe Question::Selection, type: :model do
4-
subject(:question) { described_class.new({}, options) }
4+
subject(:question) { build :selection, only_one_option:, selection_options:, is_optional:, question_text: }
55

66
let(:selection_options) { [OpenStruct.new({ name: "option 1" }), OpenStruct.new({ name: "option 2" })] }
77
let(:is_optional) { false }
88
let(:only_one_option) { "false" }
9-
let(:answer_settings) { Struct.new(:only_one_option, :selection_options).new(only_one_option, selection_options) }
10-
let(:options) do
11-
{
12-
is_optional:,
13-
answer_settings:,
14-
question_text:,
15-
}
16-
end
179
let(:question_text) { Faker::Lorem.question }
1810

1911
context "when the selection question is a checkbox" do
@@ -309,13 +301,13 @@
309301
end
310302

311303
context "when there is a none of the above question configured" do
304+
subject(:question) do
305+
build(:selection, :with_none_of_the_above_question, only_one_option:, selection_options:, is_optional:,
306+
none_of_the_above_question_is_optional:)
307+
end
308+
312309
let(:is_optional) { true }
313310
let(:none_of_the_above_question_is_optional) { "true" }
314-
let(:none_of_the_above_question) { Struct.new(:question_text, :is_optional).new(Faker::Lorem.sentence, none_of_the_above_question_is_optional) }
315-
let(:answer_settings) do
316-
Struct.new(:only_one_option, :selection_options, :none_of_the_above_question)
317-
.new(only_one_option, selection_options, none_of_the_above_question)
318-
end
319311

320312
context "when there fewer than 31 selection options" do
321313
context "when only_one_option is false" do
@@ -491,12 +483,13 @@
491483
let(:is_optional) { true }
492484

493485
context "when there is a none of the above question configured" do
494-
let(:none_of_the_above_question) { Struct.new(:question_text, :is_optional).new(Faker::Lorem.sentence, "true") }
495-
let(:answer_settings) do
496-
Struct.new(:only_one_option, :selection_options, :none_of_the_above_question)
497-
.new(only_one_option, selection_options, none_of_the_above_question)
486+
subject(:question) do
487+
build(:selection, :with_none_of_the_above_question, only_one_option:, selection_options:, is_optional:,
488+
none_of_the_above_question_is_optional:)
498489
end
499490

491+
let(:none_of_the_above_question_is_optional) { "true" }
492+
500493
it "returns true" do
501494
expect(question.has_none_of_the_above_question?).to be true
502495
end
@@ -517,9 +510,12 @@
517510
end
518511

519512
context "when the none_of_the_above_question has no question_text" do
520-
let(:answer_settings) do
521-
Struct.new(:only_one_option, :selection_options, :none_of_the_above_question)
522-
.new(only_one_option, selection_options, Struct.new)
513+
subject(:question) do
514+
build(:selection,
515+
is_optional:,
516+
only_one_option:,
517+
selection_options:,
518+
none_of_the_above_question: Struct.new)
523519
end
524520

525521
it "returns false" do

0 commit comments

Comments
 (0)