Capybara测试:js => true ...路由错误:没有路由匹配[GET]“/ assets”

时间:2013-01-31 15:41:02

标签: ruby-on-rails-3 capybara rspec-rails

当我向他们添加“: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文件中添加了一些前/后挂钩(挂钩?),我直接从接受的答案here复制。

我正在跑步:

  • Rails 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'

3 个答案:

答案 0 :(得分:45)

您丢失的路线看起来像是一些带有nil输入的自动生成的资源路径。您确定您的资产在您的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中......毕竟你丢失的路由看起来仍然像一些自动生成的资产路径,空输入)

如果上述方法没有帮助,解决方法可能是在环境/ 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

答案 1 :(得分:22)

在我的情况下,添加:

config.assets.debug = true

config/environments/test.rb修复了它。

documentation中,据说:

  

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

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

答案 2 :(得分: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