# Agile Web Development with Rails 7: Page 118 : Test fails

**URL:** <https://forum.devtalk.com/t/agile-web-development-with-rails-7-page-118-test-fails/137572>\
**Category:** PragProg Customers\
**Tags:** book-agile-web-development-with-rails-7, errata\
**Created:** [28 December 2023 18:11 UTC](https://forum.devtalk.com/t/agile-web-development-with-rails-7-page-118-test-fails/137572 "2023-12-28T18:11:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scotdb01](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/s/71c47a/32.png) [@scotdb01](https://forum.devtalk.com/u/scotdb01)\
**Post date:** [28 December 2023 18:11 UTC](https://forum.devtalk.com/t/agile-web-development-with-rails-7-page-118-test-fails/137572/1 "2023-12-28T18:11:37Z")

</div>

@rubys

Following through the book but using Rails 7.1.2.

When trying to run the test on page 118 I get -  
`

```auto
Error:
ProductsControllerTest#test_can't_delete_product_in_cart:
ActiveRecord::RecordNotDestroyed: Failed to destroy Product with id=298486374
    app/controllers/products_controller.rb:52:in `destroy'
    test/controllers/products_controller_test.rb:58:in `block (2 levels) in <class:ProductsControllerTest>'
    test/controllers/products_controller_test.rb:57:in `block in <class:ProductsControllerTest>'

bin/rails test test/controllers/products_controller_test.rb:56

Finished in 0.706042s, 39.6577 runs/s, 86.3971 assertions/s.
28 runs, 61 assertions, 0 failures, 1 errors, 0 skips

```

Is this because of the different Rails version, or is this test not meant to pass?

---

<div class="post-metadata">

**Author:** ![jabebbo](https://forum.devtalk.com/user_avatar/forum.devtalk.com/jabebbo/32/5830_2.png) [@jabebbo](https://forum.devtalk.com/u/jabebbo)\
**Post date:** [4 January 2024 19:56 UTC](https://forum.devtalk.com/t/agile-web-development-with-rails-7-page-118-test-fails/137572/2 "2024-01-04T19:56:09Z")

</div>

### @rubys

Got the same error also using Rails 7.1.2 with Ruby 3.3.0. Patched it using rescue, but would like to know the correct way to handle this.

test “can’t delete product in cart” do  
assert\_difference(“Product.count”, 0) do  
delete product\_url(products(:two))  
rescue ActiveRecord::RecordNotDestroyed  
assert true  
end  
end

---

<div class="post-metadata">

**Author:** ![novacose](https://forum.devtalk.com/user_avatar/forum.devtalk.com/novacose/32/5848_2.png) [@novacose](https://forum.devtalk.com/u/novacose)\
**Post date:** [13 January 2024 23:51 UTC](https://forum.devtalk.com/t/agile-web-development-with-rails-7-page-118-test-fails/137572/3 "2024-01-13T23:51:43Z")

</div>

@rubys

I decided to write my test like this

```auto
  test "can't delete product in cart" do
    assert_raises(ActiveRecord::RecordNotDestroyed) do
      delete product_url(products(:two))
    end

    assert Product.exists?(products(:two).id)

  end

```

---

<div class="post-metadata">

**Author:** ![joefabitz](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@joefabitz](https://forum.devtalk.com/u/joefabitz)\
**Post date:** [29 January 2024 04:07 UTC](https://forum.devtalk.com/t/agile-web-development-with-rails-7-page-118-test-fails/137572/4 "2024-01-29T04:07:13Z")

</div>

If you look in your scaffold generated ProductsController, you’ll notice that the destroy action now calls the strict `@product.destroy!` method. This change was introduced here: [Use safer 'destroy!' in controller scaffolds · rails/rails@e87608e · GitHub](https://github.com/rails/rails/commit/e87608e355a269a310ee868e73dd3efaf5611f00)

---

<div class="post-metadata">

**Author:** ![rubys](https://forum.devtalk.com/user_avatar/forum.devtalk.com/rubys/32/3797_2.png) [@rubys](https://forum.devtalk.com/u/rubys)\
**Post date:** [17 June 2024 16:52 UTC](https://forum.devtalk.com/t/agile-web-development-with-rails-7-page-118-test-fails/137572/5 "2024-06-17T16:52:24Z")

</div>

This is indeed a change in Rails 7.1, and thanks @novacose … I’ll include this change in the first update that targets a newer release of Rails.
