Faker uninitialized constant -- wrong number of arguments error

84 Views Asked by At

I just updated our app from rails 6.0 to 7.0.8 and it required me to update faker as well. This upgrade was from 1.7.3 to 2.0.0. When running our test suite, getting two errors that I'm unsure how to resolve:

/gems/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n.rb:210:in `translate': wrong number of arguments (given 2, expected 0..1) (ArgumentError)

and

Faker::Book.title[0..30]
/gems/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n.rb:210:in `translate': wrong number of arguments (given 2, expected 0..1) (ArgumentError)

As these were both working in 1.7.3, was unable to find any updates in the changelog that shows otherwise, unless it's a separate issue. Any help would be appreciated!

2

There are 2 best solutions below

1
engineersmnky On BEST ANSWER

Faker current version is 3.2.3, so 2.X is very outdated.

Your issue is ruby 3 kwargs related.

Did you also update ruby from 2.7 to 3.2?

Prior to Faker 2.10 (Pull Request) arguments were passed to I18n#translate as follows: Source

I18n.translate(*args.push(opts))

This worked up until ruby 3 (deprecation warning in 2.7) when positional and keyword arguments were separated. See Here for more details.

In Faker 2.10 this was changed to

I18n.translate(*args,**opts)

So that the keyword arguments would be passed appropriately.

In summation the simplest solution would simply be to update faker to at least 2.10 (recommended 3.X)

2
Romanch Sharma On

It seems that there is an issue with translation method in i18n gem. Instead, you can first assign the title to a variable, and then get the first 30 characters of that string. Here's how you can do it:

title = Faker::Book.title

short_title = title[0..30]

this will let the full title to translate and then it get shorted.