【问题标题】:Can't pass :locale to Capybara's method visit无法将 :locale 传递给 Capybara 的方法访问
【发布时间】:2012-03-23 13:20:35
【问题描述】:

我的 Capybaras 测试仅适用于默认语言环境,不适用于指定语言环境。

describe "How it works" do
  it "should have the content 'how it works'" do
    visit how_it_works_url(:locale => :en)
    page.should have_content('How it works')      
  end
  it "should have the content 'wie es geht'" do
    visit how_it_works_url(:locale => :de)
    page.should have_content('Wie es geht')      
  end
end  

如果 default_locale 为 'en',则第一个测试通过,如果为 'de',则第二个测试通过。

既没有翻译路线(我使用 gem 'rails-translate-routes'),也没有翻译内容。

【问题讨论】:

    标签: ruby-on-rails internationalization capybara


    【解决方案1】:

    也许这会有所帮助:

    使用capybara时路径无法正常工作

    对于 rspec,您可以将此 sn-p 放在 spec/support/locale.rb 中,例如:

    # workaround, to set default locale for ALL spec
    class ActionView::TestCase::TestController
      def default_url_options(options={})
        { :locale => I18n.default_locale }
      end
    end
    
    class ActionDispatch::Routing::RouteSet
      def default_url_options(options={})
        { :locale => I18n.default_locale }
      end
    end
    

    查看here了解更多详情。

    【讨论】:

      最近更新 更多