I use rspec, capybara. I set locale from http header as in bellow
我使用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
When I run my feature test I get error 'undefined method scan
for NilClass'. Apparently capybara don't set http headers.
当我运行我的功能测试时,我得到错误'NilClass的未定义方法扫描'。显然,水豚不设置http标头。
How I can set http header for all my features or avoid this by another way?
如何为我的所有功能设置http标头或通过其他方式避免这种情况?
2 个解决方案
#1
12
depending on your browser driver, you can set headers globally like this:
根据您的浏览器驱动程序,您可以像这样全局设置标题:
Capybara.current_session.driver.headers = { 'Accept-Language' => 'de' }
Capybara.current_session.driver.header('Accept-Language', 'de')
#2
5
You can set your headers like this:
您可以像这样设置标题:
RSpec.configure do |config|
config.before(:each) do
page.driver.header 'Accept-Language', 'de'
end
end
Source: https://github.com/thoughtbot/capybara-webkit#non-standard-driver-methods
header: set the given HTTP header for subsequent requests
header:为后续请求设置给定的HTTP头
page.driver.header 'Referer', 'https://www.thoughtbot.com'
page.driver.header'Referer','https://www.thoughtbot.com'
#1
12
depending on your browser driver, you can set headers globally like this:
根据您的浏览器驱动程序,您可以像这样全局设置标题:
Capybara.current_session.driver.headers = { 'Accept-Language' => 'de' }
Capybara.current_session.driver.header('Accept-Language', 'de')
#2
5
You can set your headers like this:
您可以像这样设置标题:
RSpec.configure do |config|
config.before(:each) do
page.driver.header 'Accept-Language', 'de'
end
end
Source: https://github.com/thoughtbot/capybara-webkit#non-standard-driver-methods
header: set the given HTTP header for subsequent requests
header:为后续请求设置给定的HTTP头
page.driver.header 'Referer', 'https://www.thoughtbot.com'
page.driver.header'Referer','https://www.thoughtbot.com'