开发者

Changing the hostname for tests in Rails

开发者 https://www.devze.com 2023-03-31 11:22 出处:网络
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/\".

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开发者_StackOverflow社区 in the tests?

Thanks.


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

request.host = "another-domain.com"


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消