Pablo Fernandez
Pablo Fernandez

Reputation: 287430

Changing the hostname for tests in Rails

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.

Upvotes: 3

Views: 3041

Answers (4)

Pablo Fernandez
Pablo Fernandez

Reputation: 287430

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

Upvotes: 1

andrykonchin
andrykonchin

Reputation: 2517

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

request.host = "another-domain.com"

Upvotes: 2

socjopata
socjopata

Reputation: 5095

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

Upvotes: 0

Matthew
Matthew

Reputation: 13332

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.

Upvotes: 0

Related Questions