简体   繁体   English

如何在RSpec中使用I18n翻译测试Ruby代码

[英]How to test Ruby code with I18n translations in RSpec

When testing Ruby code with I18n translations in RSpec, I get errors like this: 在RSpec中测试带有I18n翻译的Ruby代码时,我得到如下错误:

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. 在Rails中一切正常。

How can I use I18n in my specs? 如何在我的规格中使用I18n?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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