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

Ste*_*eve 5 ruby rspec ruby-on-rails internationalization rails-i18n

在 RSpec 中使用 I18n 翻译测试 Ruby 代码时,我收到如下错误:

translation missing: en.lib.filter.equal_to
Run Code Online (Sandbox Code Playgroud)

这是一个简化的示例:

def word_for_operator
  I18n.t('lib.filter.equal_to')
end
Run Code Online (Sandbox Code Playgroud)

规格:

it "returns the correct label" do
  expect(filter.word_for_operator).to eq("some value")
end
Run Code Online (Sandbox Code Playgroud)

Rails 中一切工作正常。

如何在我的规格中使用 I18n?

Ale*_*kin -1

下面的内容难道不能解决你这个丑陋的问题吗?我知道,\xe2\x80\x99s 不是您正在寻找的解决方案,但它可能已经足够了。

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