3

给定路线:

  scope :path => 'shipping_reports', :controller => 'ShippingReports', :as => 'shipping_reports' do
    get 'index'
  end

以及以下测试:

class ShippingReportsControllerTest < ActionController::TestCase
 test "routing" do
    assert_routing '/shipping_reports/index', { :controller => "ShippingReports", :action => "index" }
  end

  test 'index' do
    get :index
    assert_response :success
 end
end

第一次测试通过,第二次失败:

ActionController::RoutingError: No route matches {:controller=>"shipping_reports"}

有人知道我怎样才能通过该测试吗?

4

3 回答 3

0

我设法完成这项工作的唯一方法是将我的路线更改为:

controller 'shipping_reports', :path => '/shipping_reports', :as => 'shipping_reports'

它读起来更好并且测试通过但我仍然想知道如何测试问题路线......

于 2012-11-28T09:55:30.363 回答
0

有一个非常相似的问题。

给定路线:

  scope ":client" do
    resources :categories, as: :client_categories
  end

我必须执行以下操作:

test '#update' do
  put :update, { params: { id: cat.id, client: client } } # => error
  put :update, { params: { id: cat.id, } }, { client: client } # => OK
end
于 2019-04-22T19:23:47.267 回答
0

有类似的问题。我所做的是对测试路线上的 rails 预定义路径。

请参阅下面的测试修改

class ShippingReportsControllerTest < ActionController::TestCase
 test "routing" do
    assert_routing index_path, { :controller => "ShippingReports", :action => "index" }
  end

  test 'index' do
    get index_path #the actual path to the index controller
    assert_response :success
 end
end

N:B 您可以在 localhost 的路由列表中获取路径名http://localhost:3000/rails/info/routes

于 2022-02-06T13:50:58.377 回答