簡體   English   中英

Rspec與I18n

[英]Rspec with I18n

我正在嘗試測試包含一些I18n的現有應用程序

這是我的測試:

require "spec_helper"
require "rails_helper"

describe "visiting landing page" do
  it "should display the welcome text" do
    visit root_path
    expect(page).to have_text("TUTOS DUCLOS")
  end
end

我有以下錯誤:

Failures:

  1) visiting landing page should display the welcome text
     Failure/Error: visit root_path

     ActionController::UrlGenerationError:
       No route matches {:action=>"landing", :controller=>"home"} missing required keys: [:locale]
     # ./spec/features/home_spec.rb:8:in `block (2 levels) in <top (required)>'

Finished in 0.00127 seconds (files took 1.89 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/features/home_spec.rb:7 # visiting landing page should display the welcome text

如果我在"en"更改了root_path ,那么測試通過......但是我不確定這樣做是否正確...是否有更常規的導軌? 謝謝

編輯

這是我的路線:

Rails.application.routes.draw do

  scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
    get "/best_voted", to: "tutos#best_voted"
    resources :tutos
    namespace :users do
      resources :tutos
    end

    resources :tutos, only: [:show]

    resources :tutos do
      member do
        put "like", to: "tutos#upvote"
      end
    end

  as :user do
    get     "/register",  to: "devise/registrations#new", as: :register
    get     "/login",     to: "devise/sessions#new", as: :login
    get     "/logout",    to: "devise/sessions#destroy", as: :logout
    get     "/account",   to: "users#show", as: :account
    get     "/login" ,    to: "devise/sessions#new", as: :new_user_session
    post    "/login" ,    to: "devise/sessions#create", as: :user_session
    delete  "/logout" ,   to: "devise/sessions#destroy", as: :destroy_user_session
  end

    devise_for :users, skip: [:sessions]

    resources :users

    root "home#landing"
  end
  get '*path', to: redirect("/#{I18n.default_locale}/%{path}")
  get '', to: redirect("/#{I18n.default_locale}")
end

您可以將語言環境聲明為路由中的可選參數:

# config/routes.rb
scope "(:locale)", locale: /en|fr/ do
  resources :books
end

class ApplicationController < ActionController::Base
  before_action :set_locale

  private

    def set_locale
       I18n.locale = params[:locale] || I18n.default_locale
    end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM