我的测试是
it 'is not allowed to open admin/songs/index' do
get :index
expect(response).to redirect_to(new_admin_session_path)
end
失败,结果是
预计响应将重定向到http://test.host/admins/sing_in?locale=es,但重定向到http://test.host/admins/sign_in
我的问题是,它真的应该在不使用locale = es的情况下重定向到'http://test.host/admins/sign_in“。在浏览器中,将发生这种情况。
出于某种原因,Rspec将语言环境添加到new_admin_session_path中,这不是我想要的。
我的routes.rb文件是
Rails.application.routes.draw do
devise for :admins, controllers: {sessions: 'admins/sessions', registrations: 'admins/registrations'}
namespace :admin do
resources :songs
end
get('admin/home' => 'admin/home#index')
root to: 'home#index'
scope "(:locale)", locale /en|es/ do
get('home' => 'home#index')
get('songs' => 'songs#index')
end
end
我的app / controllers / application_controller.rb文件具有
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
我的config / application.rb具有
config.i18n.default_locale = :es
config.i18n.available_locales = [:en, :es]
我无权仅添加评论和指向本文的链接,因此我正在使用此空间。看看是否有帮助。 http://til.obiefernandez.com/posts/b540850342-using-defaulturloptions-in-rspec-with-rails-5