Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Wrong hostname for url helpers in rspec

Url helpers (e.g root_url) returns a different hostname in the app controllers vs rspec examples. I have successfully been able to set the domain for url helpers in my rails app like this:

class ApplicationController < ActionController::Base
  def default_url_options
    {host: "foo.com", only_path: false}
  end
end

For example, root_url outputs "http://foo.com/" within the application but "http://example.com" in my request specs. What is the recommended way to apply those url options globally in rails 3.2 so that they affect my specs? The url options do not need to be dynamic.

like image 997
Baversjo Avatar asked May 09 '12 17:05

Baversjo


3 Answers

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
like image 178
Amin Ariana Avatar answered Oct 19 '22 21:10

Amin Ariana


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
like image 5
Dan Baker Avatar answered Oct 19 '22 21:10

Dan Baker


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.

like image 2
Dan Avatar answered Oct 19 '22 20:10

Dan