我使用 rspec 和 capybara。我从 HTTP 标头中设置语言环境,代码如下:
before_filter :set_locale
def extract_locale_from_accept_language_header
request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end
def set_locale
return I18n.locale = current_user.locale if user_signed_in?
I18n.locale = extract_locale_from_accept_language_header || I18n.default_locale
end
当我运行我的功能测试时,我会得到错误信息:“undefined method 'scan' for NilClass”。显然,capybara没有设置http头。
我如何为所有的功能设置http头或通过其他方式避免这个问题?