Drew
Drew

Reputation: 2663

Setting up Capybara to have an alias for the server it boots up

So I have some features I'm wanting to test.

For a portion of the features I'm wanting to have requests placed to a different host.

When I'm testing my code locally (outside of Capybara) I'm able to use localhost:3000 for a portion of the requests, and 0.0.0.0:3000 for another portion of the requests.

Any thoughts on the best approach to replicating this approach with Capybara?

If I set the default host to localhost:3000 and spin up a server myself it works. But haven't yet been able to get it to work otherwise.

FWIW I'm using poltergeist as the js driver.

Upvotes: 0

Views: 274

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49880

The two Capybara settings that control the app under test server it starts are Capybara.server_host (defaults to 127.0.0.1) and Capybara.server_port (default to a random free port). One way to get what you want is to fix the port and then pick a domain that resolves everything to 127.0.0.1 for all hostnames (.test on your machine possibly - or add a new name to your hosts file that resolves to 127.0.0.1)

Capybara.server_port = 9999
visit('/') # will go to http://127.0.0.1:9999/
visit('http://127.0.0.1:9999')
visit('http://my_app.test:9999')

The other option is to set Capybara.always_include_port = true which will override the port for any visit call that doesn't specifically include a port with the port Capybara has run its server on (which can therefore remain a random port Capybara picks)

Capybara.always_include_port = true
visit('/') # http://127.0.0.1:<server_port>/
visit('http://my_app.test') # http://my_app.test:<server_port>
visit('http://google.com') # http://google.com:<server_port>
visit('http://google.com:80/') # http://google.com

Upvotes: 1

Related Questions