Rails语言环境搞砸了rspec

时间:2017-12-03 16:58:14

标签: ruby-on-rails rspec internationalization

我刚决定在我的rails应用程序中添加多种语言。我用这个

包裹了我的路线
  scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
    resources :courses
    # more routes
  end

我的application_controller

before_filter :set_locale

def set_locale
  I18n.locale = params[:locale] if params[:locale].present?
end

def default_url_options(options = {})
  {locale: I18n.locale}
end

在我的功能测试中,我在测试中有这个

    visit course_path(free_course)

我收到以下错误

No route matches {:action=>"show", :controller=>"courses", :id=>nil, 
:locale=>#<Course id: 4068, name: "Mabelle Kirlin III", description: "Ut voluptas eum praesentium dolore repudiandae. Eo...", main_image_url: nil, premium_course: false, slug: "mabelle-kirlin-iii", created_at: "2017-12-03 16:44:53", updated_at: "2017-12-03 16:44:53", published: true, preview_text: nil>} missing required keys: [:id]

course.id(模型)作为语言环境传入。我认为(:locale)意味着语言环境是一个可选的参数。在我的测试中是否有一种方法可以在块中设置区域设置,因此我不必将params传递给每个命名路径?

0 个答案:

没有答案