2015-07-21 97 views
2

当与RSpec中的I18n的翻译测试Ruby代码,我得到的错误是这样的:如何测试用的I18n翻译Ruby代码中的RSpec

translation missing: en.lib.filter.equal_to 

这里有一个简单的例子:

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

规格:

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

在Rails中一切正常。

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

+0

不知道我是否理解你的问题。你是说你有一个有效的':en'语言环境,但RSpec没有使用它? – Stefan

+0

@Stefan是的,这是正确的。它可以在使用Rails应用程序作为宝石时运行,或者独立运行,但不在规范中运行。 – Steve

回答

-1

难道下面解决你丑陋的问题?我明白,这不是你正在寻找的解决方案,但它可能就足够了。

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 
+0

感谢您的回答,这并不能解决我在每个测试中都需要这样做的问题。将会有不少。 – Steve