Active Record 意外的 i18n 错误
Active Record unexpected i18n error
我正在构建一个使用活动记录但不使用 rails 的 Ruby 项目。在我的一项测试中,我正在尝试以下操作:
it "fails with no driver name" do
command = "Driver"
expect {command_file.process_driver command}.to raise_error(ActiveRecord::RecordInvalid)
end
这是我尝试调用的方法
def process_driver command
driver_name = command.split[1]
Driver.create! :name => driver_name
end
我希望将 :name => nil
传递给 Driver.create!
,这应该会抛出 RecordInvalid
但我却得到 I18n::InvalidLocaleData
。这是回溯
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)>'
这是我的 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
我没有自己的语言环境文件。
知道发生了什么事吗?我没有尝试在这个项目中进行任何类型的翻译。我也不明白为什么 active_support
提供的语言环境文件会失败。如果可能的话,我很乐意以某种方式简单地禁用 i18n,但我不知道如何。知道问题出在哪里吗?
出于某种原因 :en
未设置为我的默认语言环境。我通过添加 I18n.default_locale = 'en'
:
在我的 spec_helper.rb
中解决了这个问题
I18n.default_locale = 'en' # <--- add this line
RSpec.configure do |config|
# config here...
end
我意识到这并不能解决为什么 active_support
的语言环境文件没有加载的更大问题,但我的挑战只是让错误消失,而不是使用 i18n
我正在构建一个使用活动记录但不使用 rails 的 Ruby 项目。在我的一项测试中,我正在尝试以下操作:
it "fails with no driver name" do
command = "Driver"
expect {command_file.process_driver command}.to raise_error(ActiveRecord::RecordInvalid)
end
这是我尝试调用的方法
def process_driver command
driver_name = command.split[1]
Driver.create! :name => driver_name
end
我希望将 :name => nil
传递给 Driver.create!
,这应该会抛出 RecordInvalid
但我却得到 I18n::InvalidLocaleData
。这是回溯
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)>'
这是我的 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
我没有自己的语言环境文件。
知道发生了什么事吗?我没有尝试在这个项目中进行任何类型的翻译。我也不明白为什么 active_support
提供的语言环境文件会失败。如果可能的话,我很乐意以某种方式简单地禁用 i18n,但我不知道如何。知道问题出在哪里吗?
出于某种原因 :en
未设置为我的默认语言环境。我通过添加 I18n.default_locale = 'en'
:
spec_helper.rb
中解决了这个问题
I18n.default_locale = 'en' # <--- add this line
RSpec.configure do |config|
# config here...
end
我意识到这并不能解决为什么 active_support
的语言环境文件没有加载的更大问题,但我的挑战只是让错误消失,而不是使用 i18n