简体   繁体   中英

How to test Ruby code with I18n translations in RSpec

When testing Ruby code with I18n translations in RSpec, I get errors like this:

translation missing: en.lib.filter.equal_to

Here's a simplified example:

def word_for_operator
  I18n.t('lib.filter.equal_to')
end

Spec:

it "returns the correct label" do
  expect(filter.word_for_operator).to eq("some value")
end

Everything works fine in Rails.

How can I use I18n in my specs?

Would not the following solve your ugly-looking-problem? I understand, that's not a solution you were looking for, but it could be sufficient enough.

it "returns the correct humanised label" do
    {
      'lib.quattro_filter.none' => 'None',
      'lib.quattro_filter.and' => 'and',
      ...
    }.each do |name, value|
      allow(I18n).to receive(:t).with(name).and_return(value)
    end
    # the same with expects
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM