thoughtbot/factory_bot

Syntax error in factory_bot gem files when using Ruby 2.7.0

Opened this issue · 1 comments

Description

Syntax error in factory_bot gem files when using Ruby 2.7.0.

Reproduction Steps

  1. Install Ruby 2.7.0.
  2. Create a new Rails application.
  3. Add the factory_bot_rails gem to the Gemfile.
  4. Run bundle install.
  5. Create a test file that uses FactoryBot.
  6. Run the test with rspec.

Expected Behavior

The test should run without errors.

Actual Behavior

A syntax error occurs in factory_bot gem files, such as evaluator.rb and definition_proxy.rb, preventing the test from running. The error message points to lines of code using the method_missing and sequence methods.

1. File : evaluator.rb

Correction:

Add the *args and &block parameters to the method_missing method definition

def method_missing(method_name, *args, &block)
  if @instance.respond_to?(method_name)
    @instance.send(method_name, *args, &block)
  else
    SyntaxRunner.new.send(method_name, *args, &block) 
  end
end

2. File : definition_proxy.rb

Correction:

The method_missing method definition is already correct. Change the sequence method definition to:

def sequence(name, *args, &block)
  sequence = Sequence.new(name, *args, &block)
  FactoryBot::Internal.register_inline_sequence(sequence)
  add_attribute(name) { increment_sequence(sequence) }
end

3. File : default.rb

Correction:

Change the sequence method definition to::

def sequence(name, *args, &block)
  register_sequence(Sequence.new(name, *args, &block))
end

The corrections involve adding the parameters *args and &block to the method_missing and sequence methods to ensure compatibility with Ruby 2.7.0 syntax.

System Configuration

  • factory_bot_rails version: 6.4.5
  • factory_bot version: 6.4.5
  • rails version: 6.0.0
  • ruby version: 2.7.0

#1719 will do nothing for you. The required ruby version for factory_bot is already Ruby 3.0 and you will not be able to install it with these changes. See #1614

gem 'factory_bot', '!= 6.4.5' should help you out.