当内容为html_safe时,测试视图会呈现翻译的内容吗?

时间:2012-08-20 08:57:18

标签: ruby-on-rails testing internationalization

试图找出如何编写测试,当内容是带有html的翻译键时,该测试会测试视图呈现正确的内容。为了更详细地解释一下,我有这个rspec测试:

require 'spec_helper'
describe "application/error_404.html.erb" do
  I18n.available_locales.each do |locale|
    it "should have 'not found text' in #{locale}" do
      I18n.locale = locale
      render
      rendered.should have_content I18n.t(:not_found_html)
    end
  end
end

测试它为我可用的每个语言环境呈现:not_found_html。但测试失败的原因是它查找的字符串没有html:

Failure/Error: rendered.should have_content I18n.t(:not_found_html)
       expected there to be content "Could not find the page or item you tried to view. 
         Please try again and if the problem persists please <a href=\"%{contact_link} \">let us
         know</a> so we can fix this."
       in
         "\n\t\n\t\t\tNot found\n\t\t\n\t\n\t\t\t\n\t\t\t\tCould not find the page or item you tried
         to view. Please try again and if the problem persists please let us know so we can fix
         this.\n\t\t\t\t\n\t\t\n\t"

我不知道有多少铁路知道如何使这项工作。我想要某种方式来渲染I18n字符串?对此有任何帮助将非常感激。

1 个答案:

答案 0 :(得分:0)

I18n.t(:not_found_html)只渲染字符串。

你可以这样做:

I18n.available_locales.each do |l| 
  I18n.locale = l
  I18n.translate! :not_found_html
end

如果找不到,它会引发I18n :: MissingTranslationData。