运行minitest控制器获取ActionController :: UrlGenerationError:没有路由匹配

时间:2017-06-08 08:21:26

标签: ruby-on-rails minitest ruby-on-rails-4.2

我知道,有了这个话题,更多的问题会问,但我找不到我需要的东西。

目前我正在将rails app从3.2.13更新到4.2.0,并且在升级rails之后自然会失败测试。这些测试在3.2.13中通过

所以,我有这条路线:

get '/catalogs/:article_id/get_applicability_by_brand/:brand_id', :to => 'catalogs#get_applicability_by_brand', constrains: { format: 'js' }, as: :catalog_get_applicability_by_brand

像这样的rake路线的结果:

  catalog_get_applicability_by_brand GET /catalogs/:article_id/get_applicability_by_brand/:brand_id(.:format)                                                   catalogs#get_applicability_by_brand {:constrains=>{:format=>"js"}}

控制器动作,它只渲染js.erb模板:

  def get_applicability_by_brand
      @applicability = CatalogAccess::TecDoc.get_applicability_by_brand(params[:article_id], params[:brand_id])
  end

Minitest控制器测试:

      def test_get_applicability_by_brand_action
    expected_applicability = [
      { 'model_name' => 'Model 1',
        'name' => 'fake name',
        'year_of_construct_from' => '2000',
        'year_of_construct_to' => '2010',
        'construction_type' => 'fake type' },
      { 'model_name' => 'Model 1',
        'name' => 'fake name 2',
        'year_of_construct_from' => '1991',
        'year_of_construct_to' => '2005',
        'construction_type' => 'fake type' }
    ]
    CatalogAccess::TecDoc.expects(:get_applicability_by_brand).with('12', '23').returns expected_applicability
    xhr :get, :get_applicability_by_brand, :article_id => '12', :brand_id => '23', :format => "js"
    assert_response 200
    assert_template 'get_applicability_by_brand'
    assert_template :partial => '_tecdoc2_applicability'
  end

测试错误信息是:

ActionController::UrlGenerationError:         ActionController::UrlGenerationError: No route matches {:action=>"get_applicability_by_brand", :article_id=>"12", :brand_id=>"23", :controller=>"catalogs", :format=>"js"}

我发现,如果附加到我的测试选项'use_route',它将会通过,但得到警告似乎不是很好的解决方案

xhr :get, :get_applicability_by_brand, :article_id => '12', :brand_id => '23', :format => "js", :use_route => 'catalogs'

警告讯息:

DEPRECATION WARNING: You are trying to generate the URL for a named route called "catalogs" but no such route was found. In the future, this will result in an `ActionController::UrlGenerationError` exception. (called from test_get_applicability_by_brand_action at /home/sdilshod/webapp/ps_base/apps/www/test/controllers/catalogs_controller_test.rb:627)
DEPRECATION WARNING: Passing the `use_route` option in functional tests are deprecated. Support for this option in the `process` method (and the related `get`, `head`, `post`, `patch`, `put` and `delete` helpers) will be removed in the next version without replacement. Functional tests are essentially unit tests for controllers and they should not require knowledge to how the application's routes are configured. Instead, you should explicitly pass the appropiate params to the `process` method. Previously the engines guide also contained an incorrect example that recommended using this option to test an engine's controllers within the dummy application. That recommendation was incorrect and has since been corrected. Instead, you should override the `@routes` variable in the test case with `Foo::Engine.routes`. See the updated engines guide for details. (called from test_get_applicability_by_brand_action at /home/sdilshod/webapp/ps_base/apps/www/test/controllers/catalogs_controller_test.rb:627)
DEPRECATION WARNING: You are trying to generate the URL for a named route called "catalogs" but no such route was found. In the future, this will result in an `ActionController::UrlGenerationError` exception. (called from test_get_applicability_by_brand_action at /home/sdilshod/webapp/ps_base/apps/www/test/controllers/catalogs_controller_test.rb:627)

请告诉我,请更正。

我希望你的帮助,谢谢!

0 个答案:

没有答案