3

Apparently, while running tests in Rails, the hostname is "www.example.com". If I run root_url in a test it'll return "http://www.example.com/".

Since I'm doing an app that requires another hostnames, is there a way to change the default hostname to something else, so I don't have to specify it every time I generate a URL in the tests?

Thanks.

Charles
  • 50,943
  • 13
  • 104
  • 142
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

4 Answers4

2

Path/url helpers take hostname from request object. So in test you can do something like this:

request.host = "another-domain.com"
andrykonchin
  • 2,507
  • 3
  • 18
  • 24
1

It seems the answer is no, in the test code, you have to specify the host when calling url helpers.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
0

Yes, I think in you can do something like this in your config/environments/test.rb file:

config.action_controller.asset_host = "127.0.0.1"

ps this is untested for now. I know i did something like this but once but the code is on my computer at work. I will check it tomorrow if it doesn't work.

Matthew
  • 12,892
  • 6
  • 42
  • 45
0

Well, it depends how do you test. For example, if you're using Capybara for your test suite you can specify default_host

socjopata
  • 5,028
  • 3
  • 30
  • 33