Rails: Wrong hostname for url helpers in rspec

时间秒杀一切 提交于 2019-12-04 06:25:18
Amin Ariana

In Feature specs, host! has been deprecated. Add these to your rspec_helper.rb:

# Configure Capybara expected host
Capybara.app_host = 'http://lvh.me:3000'

# Configure actual routes host during test
before(:each) do
  if respond_to?(:default_url_options)
    default_url_options[:host] = 'http://lvh.me:3000'
  end
end

Just to add to Dan's answer it would look something like this in spec_helper.rb

RSpec.configure do |config|
  # other config options
  config.before(:each) do
    host! "localhost:3000"
  end
end

You can set the host in a before block in your test in rspec:

    before :each do
      host! 'hostgoeshere.com'
    end

If you want to set it globally you could put something your spec_helper.rb file.

I tried many variations of @request.host, host!, and post path, args, {'SERVER_NAME' => my_secret_domain} without success, both as controller tests and feature tests. Very aggravating, as so many others reported success with those approaches.

The solution for me was:

request.headers["SERVER_NAME"] = my_secret_domain
post path, args

I'm running ruby 2.1.5p273, rspec 3.1.7 and Rails 4.2.0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!