42

当我向它们添加“:js => true”时,我在许多测试中都遇到了类似的错误。例如:

    An error occurred in an after hook
      ActionController::RoutingError: No route matches [GET] "/assets"
      occurred at /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/actionpack-3.2.5/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'

我之前没有在我的应用程序中测试支持 JavaScript 的东西,只是通过升级到 Capybara 2 并安装 Database Cleaner 来进行设置。config.use_transactional_fixtures = false,我已经在我的 spec_helper 文件中添加了一些之前/之后的钩子(钩子?),我直接从此处接受的答案复制了这些文件。

我在跑:

  • 导轨 3.2.5
  • Rspec-rails 2.12.2。

谁能告诉我如何攻击这个?非常感谢!

    4) Event pages 'CREATE' submitting a valid form provides a success notification and displays new event's page 
    Failure/Error: Unable to find matching line from backtrace
    ActionController::RoutingError:
    No route matches [GET] "/assets"
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/actionpack-3.2.5/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/actionpack-3.2.5/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/railties-3.2.5/lib/rails/rack/logger.rb:26:in `call_app'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/railties-3.2.5/lib/rails/rack/logger.rb:16:in `call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/actionpack-3.2.5/lib/action_dispatch/middleware/request_id.rb:22:in `call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/rack-1.4.1/lib/rack/runtime.rb:17:in `call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/activesupport-3.2.5/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/rack-1.4.1/lib/rack/lock.rb:15:in `call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/actionpack-3.2.5/lib/action_dispatch/middleware/static.rb:62:in `call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/railties-3.2.5/lib/rails/engine.rb:479:in `call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/railties-3.2.5/lib/rails/application.rb:220:in `call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/rack-1.4.1/lib/rack/builder.rb:134:in `call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/rack-1.4.1/lib/rack/urlmap.rb:64:in `block in call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/rack-1.4.1/lib/rack/urlmap.rb:49:in `each'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/rack-1.4.1/lib/rack/urlmap.rb:49:in `call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/bundler/gems/capybara-8368069cfd05/lib/capybara/server.rb:19:in `call'
    # /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/rack-1.4.1/lib/rack/handler/webrick.rb:59:in `service'
    # /Users/appletart/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
    # /Users/appletart/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
    # /Users/appletart/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
4

4 回答 4

46

您丢失的路线看起来像一些带有零输入的自动生成的资产路径。您确定您的资产在您的 css/sass 中或该路由的来源中正确指定了吗?

我一直在努力解决资产路径引发路由错误的类似问题

  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"

然后是与您的堆栈跟踪几乎相同的堆栈跟踪。在我的情况下,事实证明该资产实际上丢失了(直到最近我在我的 development.log 和我的测试环境中都没有注意到它在空闲几个月后鲁莽地“捆绑更新”了一个项目)

一路走来,我学到了很多关于 Capybara 的 app_host 和资产处理、不同环境中的资产预编译、Application.configure.assets.[debug|digest|...] 等,这可能是你应该首先搜索的地方. 或者在 sprockets/sass-rails url helper ...毕竟你丢失的路线仍然看起来像一些自动生成的资产路径,输入为空)

如果上述方法没有帮助,解决方法可能是在 environment/test.rb 中添加以下行:

config.action_dispatch.show_exceptions = true

虽然没有直接解决问题,但在我的情况下它成功地抑制了它。

另一种解决方法可能在 spec_helper.rb 中:

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

其中 myapp.dev 是 myApp 在开发模式下的一个运行实例,它提供资产或至少不会命中资产的测试路由,但可能只有在完全绝望的情况下才应该这样做。它的灵感来自于避免资产编译的策略,如 http://johnbintz.github.com/blog/2012/01/07/cucumber-capybara-asset-pipeline-debug-mode/中所述

也可能有帮助: http: //guides.rubyonrails.org/asset_pipeline.html http://rubydoc.info/github/jnicklas/capybara/master/Capybara#configure-class_method

于 2013-02-18T20:48:28.883 回答
24

就我而言,添加:

config.assets.debug = true

修复config/environments/test.rb它。

文档中,据说:

您不需要更改 test.rb。测试环境中的默认值是:config.assets.compile 为 true,config.assets.compress、config.assets.debug 和 config.assets.digest 为 false。

但是,至少在我的情况下,我需要改变它......如果有人能解释它为什么有效,我会很高兴......

于 2013-06-17T10:26:03.907 回答
1

就我而言,我得到

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

我已经在app/assets/images下放了一个空文件favicon.ico,所以开发服务器没问题。但是在这种情况下,测试环境不知道如何处理资产。我发现的解决方法是打开静态服务(而不是使用不同的服务器来服务静态资产)。惩罚当然是性能。

  in environments/test.rb, set
  config.serve_static_assets = true # default is false
于 2015-02-11T21:14:15.800 回答
0

就我而言,默认的缺失图像设置为指向 s3。我真的不在乎图像是否已加载。PhantomJs可以通过命令行 arg 禁用图像加载--load-images=no

Poltergeist中,这可以在注册驱动程序时设置:

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

于 2018-05-10T23:30:21.837 回答