Rails测试 - 路由错误

时间:2015-07-07 17:32:37

标签: unit-testing ruby-on-rails-4 integration-testing

我有一个非常奇怪的问题。

当我运行rake test时,我得到以下错误:

  1) Error:
StreamsControllerTest#test_should_get_index:
RuntimeError: In order to use #url_for, you must include routing helpers explicitly. For instance, `include Rails.application.routes.url_helpers
     test/controllers/streams_controller_test.rb:5:in `block in <class:StreamsControllerTest>'

我将以下代码添加到 /test/test_helper.rb

include Rails.application.routes.url_helpers
default_url_options[:host] = Configurable[:server_host_name]
default_url_options[:protocol] = Configurable[:server_protocol]

但我仍然得到同样的错误。

在我的测试类中,为了确定,我添加了一些调试消息:

require 'test_helper'

class StreamsControllerTest < ActionController::TestCase
  test "should get index" do
    puts ">>>> #{streams_url} <<<<"

    get :index
    assert_response :success
  end

end

我得到了预期的网址:

>>>> http://testing-end.com/streams <<<<

顺便说一句,如果我将get :index更改为get :index2(不存在),我会收到此错误:

  1) Error:
StreamsControllerTest#test_non_existing_action:
ActionController::UrlGenerationError: No route matches {:action=>"index2", :controller=>"streams"}
    test/controllers/streams_controller_test.rb:11:in `block in <class:StreamsControllerTest>'

(这对我来说很合理)

有些人知道这里发生了什么?

=================更新==================

在Integration测试文件中,我可以使用路径访问页面,例如:

require 'test_helper'

class CacheFlowsTest < ActionDispatch::IntegrationTest
  test "should get index" do
    get root_url
    assert_response :success
  end

但是 - 当执行到达浏览代码中的某个路径时,它会中断:

  1) Error:
CacheFlowsTest#test_should_get_index:
ActionView::Template::Error: arguments passed to url_for can't be handled. Please require routes or provide your own implementation
    app/views/streams/index.html.erb:51:in `block in _app_views_streams_index_html_erb__1226577901840473982_74088540'
app/views/streams/index.html.erb:38:in `_app_views_streams_index_html_erb__1226577901840473982_74088540'
test/integration/cache_flows_test.rb:5:in `block in <class:CacheFlowsTest>'

我希望这些信息有助于了解正在发生的事情。

0 个答案:

没有答案