rails: can't set default_url_options for integration tests
When writing integration tests, it would be preferable if url generation automatically made use of default_url_options as specified in ApplicationController. That said, the next best thing would be for default_url_options to be settable in the test environment, but I can’t find any way to do that. This makes it impossible (or at least extremely awkward) to run integration tests when using a scope with a parameter in it.
Here’s a simplified use case:
-
in routes.rb:
scope ':path_prefix' do resources :foos end
-
in application_controller.rb
def default_url_options { :path_prefix => 'test' } end
foos_path will now resolve to /test/foos when referenced within controllers and views.
-
in test/integration/prefix_test.rb:
require 'test_helper' class PrefixTest < ActionDispatch::IntegrationTest test "path prefix" do foos_path end end
-
When running the test (rake test:integration), I get the following error:
test_path_prefix(PrefixTest): ActionController::RoutingError: No route matches {:controller=>"foos"} /test/integration/prefix_test.rb:5:in `test_path_prefix'
In my real case, path_prefix gets its value from params[:path_prefix] unless no prefix is specified. For example, something like this:
def default_url_options
{ :path_prefix => params[:path_prefix] || 'test' }
end
About this issue
- Original URL
- State: closed
- Created 13 years ago
- Reactions: 1
- Comments: 34 (15 by maintainers)
Commits related to this issue
- Refactor the handling of default_url_options in integration tests This commit improves the handling of default_url_options in integration tests by making behave closer to how a real application opera... — committed to carlosantoniodasilva/rails by pixeltrix 12 years ago
- Refactor the handling of default_url_options in integration tests This commit improves the handling of default_url_options in integration tests by making behave closer to how a real application opera... — committed to arunagw/rails by pixeltrix 12 years ago
- Refactor the handling of default_url_options in integration tests This commit improves the handling of default_url_options in integration tests by making behave closer to how a real application opera... — committed to square/rails by pixeltrix 12 years ago
@cseelus We’re using Rails 6.0.1. Works for us when setting it in the
test_helper.rb
file (using Minitest)@nicohvi Which version of Rails did you have this problem with? Using ipoval’s solution doesn’t work with Rails 6.0.1 and 6.0.2 for me (similar Rails issue with repo to reproduce).
Recently ran into this problem, where all integration tests failed because the
ActionDipsatch#IntegrationTest
class doesn’t read fromdefault_url_options
defined in theApplicationController
.Solved using @ipoval’s solution, but setting it in
test_helper.rb
to make it more obvious.@josevalim, @spastorino The ticket says that should be a way to “set default_url_options for integration tests”.
This way looks like something what we are looking for: app.default_url_options = { :path_prefix => ‘ivan_poval’ }
Taking into account that we have the routes set like this: scope ‘:path_prefix’ do resources :foos end
The example of usage looks like this: class TestFlowTest < ActionDispatch::IntegrationTest fixtures :all
end
@josevalim, you mentioned that #default_url_options should be set through the Session object. I would argue that because the routes are global to the application and app.default_url_options = { :path_prefix => ‘ivan_poval’ } seems to do its job fine.
IMHO: the ticket should be closed without any further code changes, but there is a question about the purpose of these lines: https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/integration.rb#L187 https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/integration.rb#L188 https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/integration.rb#L189 https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/integration.rb#L190