small medium large xlarge

09 Dec 2011, 02:38
Weston Dyer (1 post)

I just finished adding the code to fix the test errors for ‘line_items’ for nil:Class and ran rake test. I get 1 Failure: test_should_destroy_cart “Cart.count” didn’t change by -1. <1> expected but was <2>.

I have been comparing my coded to the code from the book for the cart controller and i have not touched the default carts_controller_test. Can anyone point me in the right direction to track down this error ?

Thank you in advance

07 Mar 2013, 08:06
Igor Biscanin (1 post)

I have the same problem:

test “should destroy cart” do assert_difference(‘Cart.count’, -1) do session[:cart_id] = delete :destroy, :id => @cart.to_param end assert_redirected_to store_path end

Finished tests in 0.735352s, 31.2775 tests/s, 48.9561 assertions/s.

1) Failure: test_should_destroy_cart(CartsControllerTest) [c:/Sites/depot/test/functional/carts_controller_test.rb:43]: “Cart.count” didn’t change by -1. <1> expected but was <2>.

02 Apr 2013, 16:01
Psist Wu (2 posts)

I am using rails 3.2.13 and ruby 1.9.3p0, and I also encountered the same problem.

I then tried to add and destroy carts manually by web interface, but it behaved strange. For create action, when there is no cart, it creates two. For destroy action, when there is only one cart left, it destroys it but creates a new one at the same time.

The test above led me to the cause of the problem: the line in the controller @depot/app/controllers/carts_controller.rb@

before_filter :set_cart, only: [:show, :edit, :update, :destroy]

The function @before_filter@ is renamed as @before_action@ in rails 4.

The @set_cart@ function will create a new cart automatically if there is none corresponds to current session. That’s why test “@should destroy cart@” didn’t decrease the number of carts.

I simply comment out the line shown above and the problem is gone.

My solution seems contradicting to author’s intention to protect one’s cart from others, but I don’t have any better alternative for now.

Anyone could share his/her experience or opinion would be appreciated.


Later in chapter 10, the author modified the test “@should destroy cart@” and it solves the problem. However, there are still some strange behaviors in web interfaces.

You must be logged in to comment