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

Reb*_*Reb 41 capybara rspec-rails ruby-on-rails-3

当我向他们添加":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'
Run Code Online (Sandbox Code Playgroud)

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

我在跑:

  • 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'
Run Code Online (Sandbox Code Playgroud)

chr*_*gle 46

您丢失的路线看起来像是一些带有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"
Run Code Online (Sandbox Code Playgroud)

其次是堆栈跟踪几乎与您的相同.在我的情况下,事实证明资产实际上是丢失的(在我的development.log和我的测试环境中没有引起注意,直到最近我在几个闲置的几个月后不顾一切地"捆绑更新"项目)

一路上,我学到了很多关于水豚的app_host和资产处置,资产在不同的环境预编译,Application.configure.assets [调试|消化| ...]和一致好评,这可能是你应该在第一时间被搜索.或者在sprockets/sass-rails url helper中......毕竟你丢失的路由看起来仍像是一些空输入的自动生成资源路径

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

config.action_dispatch.show_exceptions = true
Run Code Online (Sandbox Code Playgroud)

虽然没有直接解决问题,但它在我的案例中成功地压制了它.

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

 ActionController::Base.asset_host = "http://myapp.dev"
Run Code Online (Sandbox Code Playgroud)

其中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


Wai*_*... 22

在我的情况下,添加:

config.assets.debug = true
Run Code Online (Sandbox Code Playgroud)

config/environments/test.rb固定它.

文档中,据说:

你不应该改变test.rb. 测试环境中的默认值为:config.assets.compile为true,config.assets.compress,config.assets.debug和config.assets.digest为false.

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

  • 有趣的是,我过去的我在现在帮助我了我:) (11认同)
  • 我也有兴趣评论为什么这个修复工作.我遇到了同样的问题,这对我有用. (3认同)
  • "调试模式禁用资产的连接和预处理." (3认同)