Ruby on rails 为什么Rspec会忽略我在application\u controller.rb中设置的默认\u url\u选项
我已经覆盖了application.rb中的方法default\u url\u选项Ruby on rails 为什么Rspec会忽略我在application\u controller.rb中设置的默认\u url\u选项,ruby-on-rails,rspec,Ruby On Rails,Rspec,我已经覆盖了application.rb中的方法default\u url\u选项 class ApplicationController < ActionController::Base protect_from_forgery with: :exception before_action :set_locale def redirect_to_root redirect_to root_path end private def default_
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :set_locale
def redirect_to_root
redirect_to root_path
end
private
def default_url_options(options={})
{ locale: I18n.locale }.merge options
end
def set_locale
if params[:locale].blank?
logger.debug "* Accept-Language: #{request.env['HTTP_ACCEPT_LANGUAGE']}"
abbr = extract_locale_from_accept_language_header
I18n.locale = if Language.find_by(abbr: abbr).nil?
logger.debug "* Unknown Language"
I18n.default_locale
else
abbr
end
logger.debug "* Locale set to '#{I18n.locale}'"
else
I18n.locale = params[:locale]
end
end
def extract_locale_from_accept_language_header
request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end
end
为什么rspec不通过我在ApplicationController中设置的defautl url选项(locale)?如何告诉rspec这样做?我遇到了类似的问题,但测试了ActionMailer类。它不是ActionController,因此未调用
default\u url\u options
我必须在配置中配置默认语言环境:
# config/environments/test.rb
Rails.application.configure do
# ...
config.action_mailer.default_url_options = {:host => 'localhost:3001', :locale => :de}
end
您的配置中可能有类似的行覆盖了url\u选项
# config/environments/test.rb
Rails.application.configure do
# ...
config.action_mailer.default_url_options = {:host => 'localhost:3001', :locale => :de}
end