Skip to content

NoMethodError when loading factory definitions that use same sequence name #1748

@timuruski

Description

@timuruski

Description

FactoryBot fails to load definitions with NoMethodError when there are at multiple factories with a sequence of the same name and at least one of them does not have a block argument.

This seems to be caused by this change #1739

Reproduction Steps

  • Define two factories that have a sequence with the same name.
  • At least one of them should have no block argument.

This script will reproduce the issue.

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"
  git_source(:github) { |repo| "https://github.com/#{repo}.git" }
  gem "factory_bot", "~> 6.0"
end

require "factory_bot"
require "minitest/autorun"
require "logger"

User = Struct.new(:number)
Department = Struct.new(:number)

FactoryBot.define do
  factory :department do
    sequence(:number) { _1 }
  end

  factory :user do
    sequence(:number) # { _1 } # Adding a block to this sequence passes tests.
  end
end

class FactoryBotTest < Minitest::Test
  def test_factory_bot_stuff
  end
end

Expected behavior

Expect factory definitions to load without error.

Actual behavior

Test crashes with this error:

NoMethodError:
  undefined method 'source_location' for nil
# /Users/timuruski/.gem/ruby/3.4.2/gems/factory_bot-6.5.2/lib/factory_bot/sequence.rb:43:in 'FactoryBot::Sequence#matches?'
# /Users/timuruski/.gem/ruby/3.4.2/gems/factory_bot-6.5.2/lib/factory_bot/definition_proxy.rb:263:in 'block in FactoryBot::DefinitionProxy#__fetch_or_register_sequence'
# /Users/timuruski/.gem/ruby/3.4.2/gems/factory_bot-6.5.2/lib/factory_bot/definition_proxy.rb:262:in 'Array#each'
# /Users/timuruski/.gem/ruby/3.4.2/gems/factory_bot-6.5.2/lib/factory_bot/definition_proxy.rb:262:in 'FactoryBot::DefinitionProxy#__fetch_or_register_sequence'
# /Users/timuruski/.gem/ruby/3.4.2/gems/factory_bot-6.5.2/lib/factory_bot/definition_proxy.rb:124:in 'FactoryBot::DefinitionProxy#sequence'
# ./example_spec.rb:22:in 'block (2 levels) in <top (required)>'
# /Users/timuruski/.gem/ruby/3.4.2/gems/factory_bot-6.5.2/lib/factory_bot/syntax/default.rb:18:in 'BasicObject#instance_eval'
# /Users/timuruski/.gem/ruby/3.4.2/gems/factory_bot-6.5.2/lib/factory_bot/syntax/default.rb:18:in 'FactoryBot::Syntax::Default::DSL#factory'
# ./example_spec.rb:21:in 'block in <top (required)>'
# /Users/timuruski/.gem/ruby/3.4.2/gems/factory_bot-6.5.2/lib/factory_bot/syntax/default.rb:37:in 'BasicObject#instance_eval'
# /Users/timuruski/.gem/ruby/3.4.2/gems/factory_bot-6.5.2/lib/factory_bot/syntax/default.rb:37:in 'FactoryBot::Syntax::Default::DSL.run'
# /Users/timuruski/.gem/ruby/3.4.2/gems/factory_bot-6.5.2/lib/factory_bot/syntax/default.rb:7:in 'FactoryBot::Syntax::Default#define'
# ./example_spec.rb:16:in '<top (required)>'

System configuration

factory_bot version: 6.5.2
ruby version: 3.4.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions