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.
This commit is contained in:
Javi Martín
2025-08-13 12:47:38 +02:00
parent a5054089b8
commit fd14c55615

View File

@@ -212,8 +212,14 @@ FactoryBot.define do
factory :poll_answer, class: "Poll::Answer" do factory :poll_answer, class: "Poll::Answer" do
question factory: [:poll_question, :yes_no] question factory: [:poll_question, :yes_no]
author factory: [:user, :level_two] author factory: [:user, :level_two]
answer { question.question_options.sample.title } option do
option { question.question_options.find_by(title: answer) } 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 end
factory :poll_partial_result, class: "Poll::PartialResult" do factory :poll_partial_result, class: "Poll::PartialResult" do