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.
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.
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