Search code examples
ruby-on-railsrspecphantomjscapybarapoltergeist

Rspec/Capybara/Poltergeist: How to stub the URL?


For my tests, I would like to stub/fake the URL that the tests are using, so that in Rails my request.host shows up as something besides 'www.example.com'

In my research, most people suggest changing capybara's app_host, but this only works when you're using the default driver. When using Poltergeist it will actually go to that URL.

Is there anything else I can use?


Solution

  • When using Capybara/Poltergeist you're doing feature tests that test the full stack. You should not be attempting to stub/mock anything when doing that (maybe you want a controller spec instead?).

    Additionally when not using the default rack-test driver you should be seeing '127.0.0.1' for request.host in your app (not www.example.com). You can change the Capybara.app_host to anything you want, but would then to need make sure your local DNS resolved that to 127.0.0.1 (/etc/hosts entry, etc). Most CI systems also allow you to specify DNS resolutions - https://circleci.com/docs/configuration/#hosts