davidwessman
davidwessman

Reputation: 1228

Rails System Test: how to set default_host_url

I am trying to run a System test where I access a external service, then get redirected back to my own application.

In this system test, how do I get access to the full url of the running test? I cannot find the current localhost port, therefore cannot send the correct redirect url to the external service.

Upvotes: 5

Views: 2052

Answers (4)

bouchard
bouchard

Reputation: 850

In Rails 7.1.0, none of the answers above would work, but this did:

module ActionDispatch
  class IntegrationTest

    def setup
      host! 'localhost:3000'
    end
  end
end

Upvotes: 1

nimmolo
nimmolo

Reputation: 571

Update for Rails 6.1.6:

The default host seems to have been 127.0.0.1. To get it to match the server host and port in my development environment, in application_system_test_case.rb I used the following:

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  def setup 
    Capybara.server_host = "localhost" # or whatever you use in dev
    Capybara.server_port = 3000 # ditto
  end

  ...

Upvotes: 0

artemave
artemave

Reputation: 6936

Rails 6. The only thing that worked for me:

# inside a subclass of ActionDispatch::IntegrationTest or ActionDispatch::SystemTestCase
setup do
  self.default_url_options = {...}
end

Upvotes: 4

davidwessman
davidwessman

Reputation: 1228

After some more searching the internets, I found an answer.

Based on this post, I added

def setup
    Rails.application.routes.default_url_options[:host] = Capybara.current_session.server.host
    Rails.application.routes.default_url_options[:port] = Capybara.current_session.server.port
end

to my application_system_test_case.rb.

This allows all _url methods to work in the system tests.

Upvotes: 5

Related Questions