Search code examples
ruby-on-railsfunctional-testing

How to test nested routes in rails


I have route:

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

How do I functional test them?

I can make get test for record right:

 test "should get index" do
    get :index, user_id: @user
    assert_response :success
    assert_not_nil assigns(:records)
  end

How do I test 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"}

Solution

  • Checkout http://guides.rubyonrails.org/testing.html for information on functional/controller tests in rails.

    A good example from the guide:

    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