Factory_bot: FactoryBot::DuplicateDefinitionError when defining the same-named sequence in the same-named trait in distinct factories

Created on 4 Feb 2019  路  6Comments  路  Source: thoughtbot/factory_bot

After upgrading to 5.0, I'm seeing this error:

Sequence already registered: __from_api_id__ (FactoryBot::DuplicateDefinitionError)

This code previously worked in 4.x. I've looked at the release notes, but there's nothing obvious there about this (at least to me as a casual user of factory_bot).

Steps to reproduce

Gemfile

# frozen_string_literal: true

source "https://rubygems.org"
gem "factory_bot", "~>5"

example.rb

require 'factory_bot'

User = Struct.new(:id)
Company = Struct.new(:id)

FactoryBot.define do
  factory :user, class: User do
    trait :from_api do
      sequence(:id, '1')
    end
  end

  factory :company, class: Company do
    trait :from_api do
      sequence(:id, '1')
    end
  end
end
$ bundle exec ruby example.rb
Traceback (most recent call last):
    18: from example.rb:6:in `<main>'
    17: from /Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot/syntax/default.rb:7:in `define'
    16: from /Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot/syntax/default.rb:49:in `run'
    15: from /Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot/syntax/default.rb:49:in `instance_eval'
    14: from example.rb:13:in `block in <main>'
    13: from /Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot/syntax/default.rb:18:in `factory'
    12: from /Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot/syntax/default.rb:18:in `instance_eval'
    11: from example.rb:14:in `block (2 levels) in <main>'
    10: from /Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot/definition_proxy.rb:174:in `trait'
     9: from /Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot/definition_proxy.rb:174:in `new'
     8: from /Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot/trait.rb:12:in `initialize'
     7: from /Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot/trait.rb:12:in `instance_eval'
     6: from example.rb:15:in `block (3 levels) in <main>'
     5: from /Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot/definition_proxy.rb:122:in `sequence'
     4: from /Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot.rb:105:in `register_sequence'
     3: from /Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot.rb:105:in `each'
     2: from /Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot.rb:106:in `block in register_sequence'
     1: from /Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot/decorator/disallows_duplicates_registry.rb:6:in `register'
/Users/shep/.rvm/gems/ruby-2.5.3/gems/factory_bot-5.0.0/lib/factory_bot/decorator.rb:10:in `method_missing': Sequence already registered: __from_api_id__ (FactoryBot::DuplicateDefinitionError)
  • Ruby Versions:

    • ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18]

    • ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-darwin18]

  • macOS 10.14.3
attached PR

Most helpful comment

I don't think that is really true

You should have a stern talking-to with the developer that opened that PR then 馃槈

All 6 comments

Thanks for the issue! That does seem unexpected. I'll try to look into this before the end of the week.

It occurred to me that this is probably because of https://github.com/thoughtbot/factory_bot/pull/1164.

It occurred to me that this is probably because of #1164.

Seems reasonable:

We register them with __#{factory_name}_#{sequence_name}__

We register them with __#{factory_name}_#{sequence_name}__

I don't think that is really true. We register them with @definition.name, which could be either a factory name or a trait name.

I don't think that is really true

You should have a stern talking-to with the developer that opened that PR then 馃槈

Thanks!

Was this page helpful?
0 / 5 - 0 ratings