Sunday, January 19, 2014

Re: [Rails] Problem testing ajax func call via member routes

Params missing check your params correctly..


Thanks & Regards,
kanna(9742665400)
...........


On Fri, Jan 17, 2014 at 3:19 AM, arca0 <arcazer0@gmail.com> wrote:
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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9458132a-ee23-47c3-b287-f75057aeebf5%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAPtiWRALFTfz9PKsnSDZz%2B86b%2BLE1h7c%3DQWGs%2ByA7rv1v91AWw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate