I have a simple model called queue_item with an attribute called "list_order" that allows users to change the order of an item's appearance on a page. I have a simple rSpec test that verifies whether the "list_order" attribute changes after a post to a controller action. My test continually fails with this error message:
QueueItemsController POST sort_list_order user is authenticated input is valid Assigns list_order
Failure/Error: expect(item1.list_order).to eq(3)
expected: 3
got: 1
This is very odd to me because when I place a binding.pry in the controller action after my code changes the "list_order" attributes, it shows that all of the queue_item objects have been successfully updated. Additionally, the UI functionality works as expected. Why is the rSpec test not reflecting the controller action's changes to the objects?
Controller Action:
def sort_list_order
counter = 1
array = params[:queue_items].sort_by{ |k,v| v }
array.each do |k,v|
item = QueueItem.find(k.to_i)
puts item.list_order
item.update(list_order:counter)
puts item.list_order
counter +=1
end
binding.pry
end
Test:
it "Assigns list_order" do
item1 = Fabricate(:queue_item)
item2 = Fabricate(:queue_item)
item3 = Fabricate(:queue_item)
post :sort_list_order, queue_items:{item1.id=>3 ,item2.id =>2 ,item3.id => 1 }
expect(item1.list_order).to eq(3)
expect(item2.list_order).to eq(2)
expect(item3.list_order).to eq(1)
end