From db69e551587e6c699767ffb39ccf1e404b17d63b Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Fri, 25 Jan 2019 14:28:15 +0100 Subject: [PATCH] Prevent CI failure caused by factories using duplicate #name attributes --- spec/factories/history/type.rb | 14 +++++++++++++- spec/factories/ticket/state.rb | 15 ++++++++++++++- spec/factories/ticket/state_type.rb | 15 ++++++++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/spec/factories/history/type.rb b/spec/factories/history/type.rb index 92aadf8a9..a6f1ea47d 100644 --- a/spec/factories/history/type.rb +++ b/spec/factories/history/type.rb @@ -1,5 +1,17 @@ FactoryBot.define do factory :'history/type', aliases: %i[history_type] do - name { Faker::Verb.past_participle } + name do + # The following line ensures that the name generated by Faker + # does not conflict with any existing names in the DB. + # There's a special syntax for this + # (Faker::Verb.unique.exclude(:past_participle, [], History::Type.pluck(:name)), + # but it's not available yet in the current release of Faker (1.9.1). + Faker::Verb.unique + .instance_variable_get(:@previous_results) + .dig([:past_participle, []]) + .merge(History::Type.pluck(:name)) + + Faker::Verb.unique.past_participle + end end end diff --git a/spec/factories/ticket/state.rb b/spec/factories/ticket/state.rb index 40841268f..ada43b2df 100644 --- a/spec/factories/ticket/state.rb +++ b/spec/factories/ticket/state.rb @@ -1,6 +1,19 @@ FactoryBot.define do factory :'ticket/state', aliases: %i[ticket_state] do - name { Faker::Verb.past_participle } + name do + # The following line ensures that the name generated by Faker + # does not conflict with any existing names in the DB. + # There's a special syntax for this + # (Faker::Verb.unique.exclude(:past_participle, [], Ticket::State.pluck(:name)), + # but it's not available yet in the current release of Faker (1.9.1). + Faker::Verb.unique + .instance_variable_get(:@previous_results) + .dig([:past_participle, []]) + .merge(Ticket::State.pluck(:name)) + + Faker::Verb.unique.past_participle + end + association :state_type, factory: :'ticket/state_type' updated_by_id { 1 } created_by_id { 1 } diff --git a/spec/factories/ticket/state_type.rb b/spec/factories/ticket/state_type.rb index 8903bd783..ba8e08996 100644 --- a/spec/factories/ticket/state_type.rb +++ b/spec/factories/ticket/state_type.rb @@ -1,6 +1,19 @@ FactoryBot.define do factory :'ticket/state_type', aliases: %i[ticket_state_type] do - name { Faker::Verb.past_participle } + name do + # The following line ensures that the name generated by Faker + # does not conflict with any existing names in the DB. + # There's a special syntax for this + # (Faker::Verb.unique.exclude(:past_participle, [], Ticket::StateType.pluck(:name)), + # but it's not available yet in the current release of Faker (1.9.1). + Faker::Verb.unique + .instance_variable_get(:@previous_results) + .dig([:past_participle, []]) + .merge(Ticket::StateType.pluck(:name)) + + Faker::Verb.unique.past_participle + end + updated_by_id { 1 } created_by_id { 1 } end