Skip to content

Keyword parameter deprecation warning when initialize_with is used with empty hash #1423

@romuloceccon

Description

@romuloceccon

Description

A factory that passes an empty hash to initialize_with will raise a deprecation warning with Ruby 2.7, regardless of how the actual initializer is defined.

Reproduction Steps

Here's test.rb:

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'
  git_source(:github) { |repo| "https://github.com/#{repo}.git" }
  gem 'factory_bot', git: 'https://github.com/thoughtbot/factory_bot',
      ref: '6caa2abb56d21e60a408e3765195708da5d94519'
end

require 'factory_bot'

class Test
  def initialize(arg)
    puts arg.inspect
  end
end

FactoryBot.define do
  factory :test1, class: Test do
    initialize_with { new({ 'k' => 'v' }) }
  end
  factory :test2, class: Test do
    initialize_with { new({}) }
  end
end

include FactoryBot::Syntax::Methods

Test.new({ 'k' => 'v' })
Test.new({})
build(:test1)
build(:test2) # warning is raised here

Expected behavior

No warnings.

Actual behavior

$ ruby test.rb
Fetching https://github.com/thoughtbot/factory_bot
Fetching gem metadata from https://rubygems.org/........
Resolving dependencies...
Using concurrent-ruby 1.1.6
Using i18n 1.8.3
Using minitest 5.14.1
Using thread_safe 0.3.6
Using tzinfo 1.2.7
Using zeitwerk 2.4.0
Using activesupport 6.0.3.2
Using bundler 2.1.4
Using factory_bot 6.1.0 from https://github.com/thoughtbot/factory_bot (at 6caa2ab@6caa2ab)
{"k"=>"v"}
{}
{"k"=>"v"}
.../ruby-2.7.1/lib/ruby/gems/2.7.0/bundler/gems/factory_bot-6caa2abb56d2/lib/factory_bot/decorator.rb:16: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
.../ruby-2.7.1/lib/ruby/gems/2.7.0/bundler/gems/factory_bot-6caa2abb56d2/lib/factory_bot/decorator/invocation_tracker.rb:10: warning: The called method `method_missing' is defined here
.../ruby-2.7.1/lib/ruby/gems/2.7.0/bundler/gems/factory_bot-6caa2abb56d2/lib/factory_bot/decorator/new_constructor.rb:9: warning: Passing the keyword argument as the last hash parameter is deprecated
test.rb:13: warning: The called method `initialize' is defined here
{}

System configuration

$ uname -srvmo
Linux 4.19.0-9-amd64 #1 SMP Debian 4.19.118-2+deb10u1 (2020-06-07) x86_64 GNU/Linux
$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
$ gem --version
3.1.4

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