I use rspec, capybara. I set locale from http header as in bellow
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.
How I can set http header for all my features or avoid this by another way?
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')
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
page.driver.header 'Referer', 'https://www.thoughtbot.com'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With