Rails 3.1 installs a host in a test environment

I get http://www.example.com whenever I use root_url in my tests.

It works great in development, where I have this in config / environment / development.rb:

 Rails.application.routes.default_url_options[:host]= 'localhost:3000' 

However, adding this does not work in config / environment / test.rb. What should be added to use localhost:3000 as a host in a test environment?

+4
source share
1 answer

Testing code, which depends on default_url_options , causes all kinds of problems, see this thread and this issue for examples.

I solved the problem by correcting ActionDispatch::Routing::RouteSet in the tests to force the rails to include default values ​​for any parameters I want (in my case, locale ). See my answer to the github issue related to above for more details.

To override the host parameter using the same approach:

 class ActionDispatch::Routing::RouteSet def url_for_with_host_fix(options) url_for_without_host_fix(options.merge(:host => 'localhost:3000')) end alias_method_chain :url_for, :host_fix end 

Put this in a file in support should do the trick.

+5
source

Source: https://habr.com/ru/post/1444500/


All Articles