Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8010401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:43:29+00:00 2026-06-04T18:43:29+00:00

My destroy unit test for line_item model is failing with the error "Couldn’t find

  • 0

My destroy unit test for line_item model is failing with the error "Couldn’t find Product with id=1". It seems that Rails can’t destroy my line_item because it throws an exception when getting it from the database. Here is my LineItem model:

class LineItem < ActiveRecord::Base
  belongs_to :product
  belongs_to :cart
  
  def total_price
    product.price * quantity
  end
end

And here is the test suite:

require 'test_helper'

class LineItemsControllerTest < ActionController::TestCase
  setup do
    @line_item = line_items(:one)
    Rails.logger.debug(@line_item.to_yaml)
    Rails.logger.debug(Product.all.to_yaml)
  end

  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:line_items)
  end

  test "should get new" do
    get :new
    assert_response :success
  end

  test "should create line_item" do
    assert_difference('LineItem.count') do
      post :create, product_id: products(:ruby).id
    end

    assert_redirected_to cart_path(assigns(:line_item).cart)
  end

  test "should show line_item" do
    get :show, id: @line_item
    assert_response :success
  end

  test "should get edit" do
    get :edit, id: @line_item
    assert_response :success
  end

  test "should update line_item" do
    put :update, id: @line_item, line_item: @line_item.attributes
    assert_redirected_to line_item_path(assigns(:line_item))
  end

  test "should destroy line_item" do
    Rails.logger.debug "Breaking!"
    assert_difference('LineItem.count', -1) do
      delete :destroy, id: @line_item
    end

    assert_redirected_to cart_path(path)
  end
end

And here is the part that I have logged:

    Breaking!
  [1m[35m (0.1ms)[0m  SELECT COUNT(*) FROM "line_items" 
Processing by LineItemsController#destroy as HTML
  Parameters: {"id"=>"980190962"}
  [1m[36mLineItem Load (0.1ms)[0m  [1mSELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? LIMIT 1[0m  [["id", "980190962"]]
  [1m[35mProduct Load (0.1ms)[0m  SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1  [["id", 1]]
Completed 500 Internal Server Error in 3ms
  [1m[36m (0.1ms)[0m  [1mrollback transaction[0m
  [1m[35m (0.1ms)[0m  begin transaction
  [1m[36mLineItem Load (0.1ms)[0m  [1mSELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? LIMIT 1[0m  [["id", 980190962]]
--- !ruby/object:LineItem
attributes:
  id: 980190962
  product_id: 1
  cart_id: 1
  created_at: 2012-05-25 20:37:17.000000000 Z
  updated_at: 2012-05-25 20:37:17.000000000 Z
  quantity: 1
  product_price: 

  [1m[35mProduct Load (0.2ms)[0m  SELECT "products".* FROM "products" 
---
- !ruby/object:Product
  attributes:
    id: 207281424
    title: Programming Ruby 1.9
    description: Ruby is the fastest growing and most exciting dynamic language out
      there.  If you need to get working programs delivered fast, you should add Ruby
      to your toolbox.
    image_url: ruby.png
    price: 49.5
    created_at: 2012-05-25 20:37:17.000000000 Z
    updated_at: 2012-05-25 20:37:17.000000000 Z  

EDIT: here is the Product model:

class Product < ActiveRecord::Base
  has_many :line_items
  
  before_destroy :ensure_not_referenced_by_any_line_item
  
  # Validation
  validates :title, :description, :image_url, presence: true
  validates :title, length: {
    minimum: 10,
    message: "must be at least %{count} characters long"
  }
  validates :price, numericality: { greater_than_or_equal_to: 0.01 }
  validates :title, uniqueness: true
  validates :image_url, allow_blank: true, format: {
    with: %r{\.(gif|jpg|png)$}i,
    message: 'must be a URL for GIF, JPG or PNG image.'
  }
  
  private
  
  def ensure_not_referenced_by_any_line_item
    if line_items.empty?
      return true
    else
      errors.add :base, 'Line items present'
      return false
    end
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:43:30+00:00Added an answer on June 4, 2026 at 6:43 pm

    This is one of the reasons that factories are generally favored over fixtures in the rails community. Fixtures don’t automatically load their associations and tend to be brittle because of it. Your fixture has a product_id of 1, but that product doesn’t exist.

    I’m not sure exactly how to fix your particular issue, but I would suggest you either:

    1. Create a Product with id=1 in your test.
    2. Create a Product fixture with id=1 and load it in your setup
    3. Switch to factories (preferably this one)

    EDIT

    As described here, you could also use label references for associations. So if you had a product fixture called ‘tv’, you could delete your line_item fixture’s product_id field and replace it with product: tv

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of Test::Unit tests for a Rails application. It was developed
I want to create and then destroy buttons, all with code, but can't find
I am doing unit testing and I expect that all data committed to the
I want to write a few unit tests that do not make any changes
I'm unit testing my Java/Jersey web service and running into an interesting test case.
How would go about writing proper unit testing (and integration testing for that matter)
With Rails 3.1, I have: class Status < ActiveRecord::Base has_many :participations, :dependent => :destroy
I want to destroy all objects that have parent_type == 'Profile' or child_type ==
class Product < ActiveRecord::Base has_many :models, :dependent => :destroy, :order => 'display, title' class
How do I destroy instance variable after completing one ajax call. In my app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.