如何在rails中测试嵌套路由

时间:2013-06-13 14:15:39

标签: ruby-on-rails functional-testing

我有路线:

  resources :users do
    resources :records do
      resources :grades
    end
  end

我如何对它们进行功能测试?

我可以getrecord进行 test "should get index" do get :index, user_id: @user assert_response :success assert_not_nil assigns(:records) end 测试:

post

如何测试assert_difference('Record.count') do post :create, record: { comment: @record.comment, device: @record.device, status: @record.status } end # I will get error ActionController::RoutingError: No route matches {:record=>{:comment=>"MyText", :device=>"MyString", :status=>"pending"}, :controller=>"records", :action=>"create"}

{{1}}

1 个答案:

答案 0 :(得分:0)

结帐http://guides.rubyonrails.org/testing.html以获取有关rails中功能/控制器测试的信息。

指南中的一个很好的例子:

test "should create record" do
  assert_difference('Record.count') do
    post :create, record: { comment: 'Some comment', device: 'Some device', status: 'Some status'}, user_id: @user.id
  end

  assert_redirected_to user_record_path(@user.id, assigns(:record))
end