Removing the :params
and :lang
from the default_url_options
did the trick.
Rails 4 Mounted Engine Rspec Url adding params twice
-
30-07-2022 - |
Pergunta
I have a Rails 4 mounted engine MyEngine
Inside a lib
module I'm using the url_helper
to find a route.
MyEngine::Engine.routes.url_helpers.send("test_controller_url",{:id => 1, :lang => I18n.locale})
And I have configured rspec
with the default_url_options
in spec/spec_helper.rb
like this.
class ActionView::TestCase::TestController
def default_url_options(options={})
{ :params => { :lang => I18n.default_locale }, :host => "test.host" }.merge options
end
end
class ActionDispatch::Routing::RouteSet
def default_url_options(options={})
{ :params => { :lang => I18n.default_locale }, :host => "test.host" }.merge options
end
end
The resultant url
I'm expecting from the call is:
http://test.host/controller/1/test?lang=en
But I'm getting the url
:
http://test.host/?lang=en/controller/1/test?lang=en
I did some debugging into actionpack/action_dispatch/http/url.rb
in actionpack-4.0.1
of rails
.
The options that come to the url_for
method are:
{
:params=>{:lang=>:en},
:host=>"test.host",
:use_route=>"my_engine",
:only_path=>true,
:lang => :en,
:path=>"/",
:script_name=>nil,
:user=>nil,
:password=>nil
}
and
{
:params=>{:lang=>:en},
:host=>"test.host",
:action=>"test",
:controller=>"my_engine/controller",
:use_route=>"test_controller",
:only_path=>false,
:id=>1,
:path=>"/controller/1/test",
:script_name=>"/?lang=en",
:user=>nil,
:password=>nil
}
And in the dummy
rails application inside spec/dummy/config/routes.rb
I've mounted the engine
with:
mount MyEngine::Engine, :at => "/"
It seems to produce the malformed url
only with rspec
and not even with rails console
I also noticed that there are two calls being made to url_for
, one for the engine
and one for the actual path
.
What am I doing wrong here? Is it a configuration thing or is there an actual bug?
Solução