Search code examples
rubyactiverecordinternationalizationactivesupport

Active Record unexpected i18n error


I am building a Ruby project that uses active record but not rails. Inside one of my tests I am trying the following:

it "fails with no driver name" do
  command = "Driver"
  expect {command_file.process_driver command}.to raise_error(ActiveRecord::RecordInvalid)
end

And here is the method I am trying to call

def process_driver command
  driver_name = command.split[1]
  Driver.create! :name => driver_name
end

I expect to be passing :name => nil to Driver.create! which should throw a RecordInvalid but instead I get I18n::InvalidLocaleData. Here is the backtrace

expected ActiveRecord::RecordInvalid, got #<I18n::InvalidLocaleData: can not load translations from /Users/me/.rbenv/versions/2.3.1/lib/r...ems/activesupport-5.1.3/lib/active_support/locale/en.yml: expects it to return a hash, but does not> with backtrace:
  # ./command_file.rb:81:in `process_driver'
  # ./command_file.rb:63:in `block in process'
  # ./command_file.rb:51:in `each'
  # ./command_file.rb:51:in `each_with_index'
  # ./command_file.rb:51:in `process'
  # ./spec/command_file_spec.rb:60:in `block (5 levels) in <top (required)>'
  # ./spec/command_file_spec.rb:60:in `block (4 levels) in <top (required)>'
  # ./spec/spec_helper.rb:75:in `block (3 levels) in <top (required)>'
  # ./spec/spec_helper.rb:74:in `block (2 levels) in <top (required)>'

And here is my Gemfile

source 'https://rubygems.org'

gem 'sqlite3', '~> 1.3', '>= 1.3.13'
gem 'activerecord', '~> 5.1', '>= 5.1.3'
gem 'pry', '~> 0.10.4'
gem 'rspec', '~> 3.6'
gem 'factory_girl', '~> 4.5'

group :test do
  gem 'database_cleaner'
end

I have no locale files of my own.

Any idea what's going on? I am not attempting any kind of translation in this project. I also don't understand why a locale file provided by active_support should fail. I'd be happy to simply disable i18n somehow if that were possible but I don't know how. Any ideas what the problem is?


Solution

  • For what ever reason :en was not set as my default locale. I fixed that in my spec_helper.rb by adding I18n.default_locale = 'en':

    I18n.default_locale = 'en' # <--- add this line
    
    RSpec.configure do |config|
        # config here...
    end
    

    I realize this doesn't fix the larger problem of why the locale file from active_support was not loading, but my challenge was simply to make the error go away, not to use i18n