Capybara tests with :js=>true… Routing Error: No route matches [GET] “/assets”

半世苍凉 提交于 2019-11-28 04:39:40

Your missing route looks like some auto-generated assets path with nil input. Are you sure your assets are correctly specified in your css/sass or wherever this route comes from?

I have been struggling with a similiar issue where an asset path induced a routing error

  1) user signup: [ JS ] : creates User on successful signup
     Failure/Error: Unable to find matching line from backtrace
     ActionController::RoutingError:
       No route matches [GET] "/assets/images/leftArrow.png"

followed by a stack trace virtually identical to yours. In my case it turned out the asset was in fact missing (which went unnoticed in my development.log and in my test environment until recently I recklessly 'bundle updated' an project after several idle months)

Along the way I learned a lot about Capybara's app_host and asset handling, assets precompiling in different environments, Application.configure.assets.[debug|digest|...] and alike, which probably is where you should be searching in the first place. Or in the sprockets/sass-rails url helper ... after all your missing route still looks like some auto-generated assets path with empty input)

If the above doesn't help a workaround might be adding the following line in environments/test.rb:

config.action_dispatch.show_exceptions = true

While not addressing the problem directly, it successfully suppressed it in my case.

Another kind of workaround could be in spec_helper.rb:

 ActionController::Base.asset_host = "http://myapp.dev"

where myapp.dev is a running instance of myApp in development mode, which delivers the assets or at least does not hit your test routing for the assets, but probably one should do this only in complete desparation. It's inspired by the strategy to avoid asset compiling as explained in http://johnbintz.github.com/blog/2012/01/07/cucumber-capybara-asset-pipeline-debug-mode/

Also potentially helpful: http://guides.rubyonrails.org/asset_pipeline.html http://rubydoc.info/github/jnicklas/capybara/master/Capybara#configure-class_method

In my case, adding:

config.assets.debug = true

to config/environments/test.rb fixed it.

In the documentation, it is said that:

You should not need to change test.rb. The defaults in the test environment are: config.assets.compile is true and config.assets.compress, config.assets.debug and config.assets.digest are false.

But, at least in my case, I needed to change it... If anyone could explain exactly why it works, I would be glad...

In my case, I am getting

 Failure/Error: Unable to find matching line from backtrace
 ActionController::RoutingError:
   No route matches [GET] "/favicon.ico"

I have already put an empty file favicon.ico under app/assets/images, so the development server is fine. But the test environment doesn't know how to handle the assets in this case. The workaround I found was to turn on static serving (instead of using a different server to serve static assets). The penalty of course is the performance.

  in environments/test.rb, set
  config.serve_static_assets = true # default is false

In my case, the default missing image was set to point to s3. I didn't really care if the image was loaded or not. PhantomJs has an option to disable image loading via the command line arg --load-images=no.

In Poltergeist, this can be setup when registering the driver:

Capybara.register_driver :poltergeist do |app| Capybara::Poltergeist::Driver.new(app, :phantomjs_options => ['--load-images=no']) end

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