Ruby-on-rails 将 I18n 翻译添加到 rspec 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5415158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:45:57  来源:igfitidea点击:

Add I18n translations to rspec tests

ruby-on-railsrspec

提问by Spyros

How can i add a translation test to my specs ? Something like :

如何将翻译测试添加到我的规格中?就像是 :

flash[:error].should == I18n.translate 'error.discovered'

this does not work of course. How to make it work ?

这当然行不通。如何使它工作?

I want to make sure that i get a certain error back.

我想确保我得到某个错误。

回答by nathanvda

In my code, a Rails 3 project, using RSpec 2, that is exactly the line I write:

在我的代码中,一个 Rails 3 项目,使用 RSpec 2,这正是我写的行:

describe "GET 'index'" do
  before do
    get 'index'
  end
  it "should be successful" do
    response.should be_redirect
  end

  it "should show appropriate flash" do
    flash[:warning].should == I18n.t('authorisation.not_authorized')
  end
end

So I am not sure why you say it is not possible?

所以我不确定你为什么说这是不可能的?

回答by Paul Fioravanti

Not sure if this is optimal, but in my Rails3/RSpec2 apps, I test all my locale translations in RSpec in the following way:

不确定这是否是最佳的,但在我的 Rails3/RSpec2 应用程序中,我通过以下方式测试 RSpec 中的所有语言环境翻译:

I set the available locales in my config/initializers/i18n.rbfile:

我在config/initializers/i18n.rb文件中设置了可用的语言环境:

I18n.available_locales = [:en, :it, :ja]

and in my spec files that need translation checking I have tests that looks something like:

在我需要翻译检查的规范文件中,我有如下测试:

describe "Example Pages" do   

  subject { page }

  I18n.available_locales.each do |locale|

    describe "example page" do
      let(:example_text) { t('example.translation') }

      before { visit example_path(locale) }

      it { should have_selector('h1', text: example_text) }
      ...
    end
    ...   
  end
end

I wasn't sure how to get just the t()method to be usable in the specs without needing I18n.tso I just added a small convenience method to spec/support/utilities.rb:

我不确定如何t()在不需要的情况下I18n.t让方法在规范中可用,所以我只是在spec/support/utilities.rb 中添加了一个小的方便方法:

def t(string, options={})
  I18n.t(string, options)
end

Update: These days I tend to use the i18n-tasksgem to handle testing related to i18n, and not what I wrote above or have answered on StackOverflow previously.

更新:这些天我倾向于使用i18n-tasksgem 来处理与 i18n 相关的测试,而不是我上面写的或之前在 StackOverflow 上回答的内容。

I wanted to use i18n in my RSpec tests primarily to make sure that I had translations for everything ie there were no translations missed. i18n-tasks can do that and more through static analysis of my code, so I don't need to run tests for all I18n.available_localesanymore (apart from when testing very locale-specific functionality, like, for example, switching from any locale to any other locale in the system).

我想在我的 RSpec 测试中使用 i18n,主要是为了确保我对所有内容都有翻译,即没有遗漏任何翻译。i18n-tasks 可以通过对我的代码进行静态分析来做到这一点,所以我不再需要为所有运行测试I18n.available_locales(除了测试非常特定于语言环境的功能时,例如,从任何语言环境切换到任何其他语言环境系统中的区域设置)。

Doing this has meant I can confirm that all i18n keys in the system actually have values (and that none are unused or obsolete), while keeping the number of repetitive tests, and consequently the suite running time, down.

这样做意味着我可以确认系统中的所有 i18n 键实际上都有值(并且没有一个是未使用或过时的),同时保持重复测试的数量,从而降低套件运行时间。

回答by zetetic

Assuming the code in the controller is:

假设控制器中的代码是:

flash[:error] = I18n.translate 'error.discovered'

You could stub 'translate':

你可以存根“翻译”:

it "translates the error message" do
  I18n.stub(:translate) { 'error_message' }
  get :index # replace with appropriate action/params
  flash[:error].should == 'error_message'
end