From fd14c55615e4484fccbeace1c1b7737ec87adb40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 13 Aug 2025 12:47:38 +0200 Subject: [PATCH] Make answer depend on the option in poll answer factory Until now, when writing `create(:poll_answer, option: option)`, the `answer` field would take the title of a random option instead of taking the title from the `option` variable. So now, if we're given the option, the `answer` field will be taken from the option itself. Note that writing something like: ``` option { question.question_options.find_by(title: answer) } answer { option.title } ``` Would create an infinite loop when creating a poll answer if we don't pass the `option` and/or the `answer` attributes. So, instead, we're making the `option` depend on the `answer` attribute exclusively when we pass the `answer` attribute to the factory. --- spec/factories/polls.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/spec/factories/polls.rb b/spec/factories/polls.rb index 735c20cb8..100029ec7 100644 --- a/spec/factories/polls.rb +++ b/spec/factories/polls.rb @@ -212,8 +212,14 @@ FactoryBot.define do factory :poll_answer, class: "Poll::Answer" do question factory: [:poll_question, :yes_no] author factory: [:user, :level_two] - answer { question.question_options.sample.title } - option { question.question_options.find_by(title: answer) } + option do + if answer + question.question_options.find_by(title: answer) + else + question.question_options.sample + end + end + after(:build) { |poll_answer| poll_answer.answer ||= poll_answer.option&.title } end factory :poll_partial_result, class: "Poll::PartialResult" do