现在我正在用rails i18n翻译我的rails应用程序。
但是已经通过的路径测试显示错误,因为i18n改变了路由。下面的错误是其中一个错误。
我认为我的应用程序运行良好,所以我的测试有一些错误,但我想知道如何修复它。
有什么办法吗?
错误
ERROR["test_should_redirect_edit_when_not_logged_in", UsersControllerTest, 3.4624758772552013]
test_should_redirect_edit_when_not_logged_in#UsersControllerTest (3.46s)
ActionController::UrlGenerationError:
ActionController::UrlGenerationError: No route matches {:action=>"edit", :controller=>"users", :locale=>#<User id: 762146111,
name: "Michael Example", email: "michael@example.com", created_at: "2022-11-09 09:53:30", updated_at: "2022-11-09 09:53:30",
password_digest: "$2a$04$r.WTFttwDcJsuUdBSHQDi.IvFzECkpL37SEdlAKzpdX...", remember_digest: nil,
admin: true, activation_digest: nil, activated: true, activated_at: "2022-11-09 09:53:30", reset_digest: nil, reset_sent_at: nil>},
missing required keys: [:id], possible unmatched constraints: [:locale]
test/controllers/users_controller_test.rb:21:in `block in <class:UsersControllerTest>'
与错误相关的代码
- routes.rb
Rails.application.routes.draw do
scope "(:locale)", locale: /en | ja/ do
root 'static_pages#home'
get '/:locale' => 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/search', to: 'movies#search'
get '/show', to: 'movies#show'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
post '/guest_login', to: 'guest_sessions#create'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users do
member do
get :following, :followers
end
end
resources :movies
resources :account_activations, only: [:edit]
resources :resend_activations, only: [:new, :create]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :comments do
resource :comments, only: [:create, :new, :edit, :update, :destroy]
resource :favorites, only: [:create, :destroy]
end
resources :relationships, only: [:create, :destroy]
end
end
- application.rb
require_relative 'boot'
require 'rails/all'
Bundler.require(*Rails.groups)
module Moviest
class Application < Rails::Application
config.load_defaults 5.1
config.i18n.load_path += Dir[Rails.root.join('my/locales/*.{rb,yml}')]
config.i18n.default_locale = :ja
config.action_view.embed_authenticity_token_in_remote_forms = true
config.time_zone = 'Tokyo'
config.active_record.default_timezone = :local
end
end
1.应用程序控制器.rb
class ApplicationController < ActionController::Base
before_action :set_request
protect_from_forgery with: :exception
include SessionsHelper
def set_request
Thread.current[:request] = request
end
around_action :switch_locale
def switch_locale(&action)
locale = params[:locale] || I18n.default_locale
I18n.with_locale(locale, &action)
end
def default_url_options
{ locale: I18n.locale }
end
〜〜〜〜
end
1.用户控制器测试.rb
require 'test_helper'
class UsersControllerTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
@other_user = users(:archer)
end
~
test "should redirect edit when not logged in" do
get edit_user_path(@user) #error point
assert_not flash.empty?
assert_redirected_to login_url
end
~
end
1条答案
按热度按时间lsmepo6l1#
我修正了在测试文件上定义语言环境并在调用@user时调用它的错误。发生这个错误是因为基于Rails教程的测试使用了测试驱动开发,这意味着语言环境需要在测试文件上定义。无论如何,谢谢你。