Rails:如何使用作用域定义的路径测试控制器?

时间:2012-11-28 08:47:16

标签: ruby-on-rails routes functional-testing

鉴于路线:

  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"}

任何人都知道如何让测试通过?

2 个答案:

答案 0 :(得分:0)

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

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

它读得更好,测试通过,但我仍然想知道如何测试问题路线...

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