Problem testing ajax func call via member routes

Hi, I’m having a problem writing a proper test for an exercise from “Agile Web Development with Rails 4.0”. The exercise was about creating a member route, which was then used via ajax to decrement an cart item (ListItem) attribute, eventually destroying it when it hits 0. The route I wrote is:

resources :line_items do

member do

post 'decrement'

end

end

Controller function ‘decrement’ uses a params[:id] for internal processing.

The test I wanted to write was to check if the controller ‘decrement’ function indeed destroys the LineItem when its quantity (line_item.quantity) hits zero. I’ve therefore created a fixture:

one:

product_id: 0

cart_id: 0

quantity: 1

and assigned it to @line_item in my ‘line_items_controller_test.rb’, where I wrote this test:

test “should destroy line_item via ajax” do

assert_difference(‘LineItem.count’) do

xhr :post, :decrement, id: @line_item

end

end

However, this throws the following error in my face:

LineItemsControllerTest#test_should_destroy_line_item_via_ajax:

NoMethodError: undefined method `id’ for nil:NilClass

app/controllers/line_items_controller.rb:16:in `decrement'

test/controllers/line_items_controller_test.rb:61:in `block (2 levels) in <class:LineItemsControllerTest>'

test/controllers/line_items_controller_test.rb:60:in `block in <class:LineItemsControllerTest>'

I tried variations of different parameters to xhr but with no success. I’ve also tried fixing the route passed by consulting rake routes, but it appears I don’t really understand how this works. Any help would be deeply appreciated.

Thanks,

arca0

Params missing check your params correctly…