path not working properly when using capybara

六月ゝ 毕业季﹏ 提交于 2019-12-21 09:08:44

问题


I'm using rails 3.0.5, rspec2 with latest capybara.

Routes setup like:

scope "(:locale)", :locale => /de|fr|it|en/ do
  resources :dossiers
end

In application_controller I have this:

def default_url_options(options={})
  options[:locale] = "es"
  options
end

So in my views I can use

link_to 'test', dossier_path(1)

without any problems.

But when I do the same in capybara's visit it tries to use the 1 for the locale and not for the id. It only works when I use

visit dossier_path(nil, 1)

or

visit dossier_path(:id => 1)

But both are ugly and looks like a dirty hack. So why do I need to use this dirty hack and what do I jave to do, so that I can use the path methods just like in the views (so without the dirty hack of having to add nil or explicitly pass :id => ...)? :)


回答1:


I ran into a similar issue. You can set the default_url_options in a before block like this in request specs:

 before :each do
   app.default_url_options = { :locale => :es }
 end



回答2:


Unfortunately the route generation happens outside of Application Controller. And Capybara doesn't do any magic to provide default url options from it to route helpers.

But you can specify default locale inside your routes.rb

scope "(:locale)", :locale => /de|fr|it|en/, :defaults => { :locale => "es" } do
  resources :dossiers
end

And now if you don't pass :locale option to a route helper it will default to "es". Actually, it isn't necessary to keep def default_url_options anymore in your controller.




回答3:


I'm running rails 3.2.6 and I use a technique that I found here https://github.com/rspec/rspec-rails/issues/255 under Phoet's comment. Just put this somewhere in /spec/support and it should cover all your specs

class ActionView::TestCase::TestController
  def default_url_options(options={})
    { :locale => I18n.default_locale }
  end
end

class ActionDispatch::Routing::RouteSet
  def default_url_options(options={})
    { :locale => I18n.default_locale }
  end
end



回答4:


Opposite as shown here under Using Capybara with RSpec the only way I've been able to get it working is writing

visit user_path(:id => myuser.id.to_s)

so for you it should be

visit dossier_path(:id => "1")

Does it work?



来源:https://stackoverflow.com/questions/5396866/path-not-working-properly-when-using-capybara

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!