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

  • Home
  • SEARCH
  • 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 4614762
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:44:44+00:00 2026-05-22T01:44:44+00:00

Howdy, I’m having a bit of a problem getting my unit tests to pass.

  • 0

Howdy, I’m having a bit of a problem getting my unit tests to pass. Specifically, the ones in *cart_test.rb* are failing.

  test "add two different items" do
    cart = Cart.create
    book_one = products(:one)
    book_two = products(:two)
    cart.add_product(book_one.id).save!
    cart.add_product(book_two.id).save!
    assert_equal 2, cart.line_items.size
    assert_equal book_one.price + book_two.price, cart.total_price
  end

  test "add two unique items" do
    cart = Cart.create
    ruby_book = products(:ruby)
    cart.add_product(ruby_book.id).save!
    cart.add_product(ruby_book.id).save!
    assert_equal 2*ruby_book.price, cart.total_price
    assert_equal 1, cart.line_items.size
    assert_equal 2, cart.line_items[0].quantity
  end

Here’s my repository: https://github.com/zackster/Agile-Web-Development-w–Rails—-DEPOT-application

Could somebody please help me understand what’s going on? I believe the tests are failing because the items aren’t actually being added to the cart, but it could be something else entirely [I’m a rails novice ]. . . thanks!

  • 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-05-22T01:44:44+00:00Added an answer on May 22, 2026 at 1:44 am

    The CartTest tests are failing because in Cart::add_product, you aren’t actually tying a new line_item to a cart — just creating orphaned line_items with a raw LineItem.create. To tie to a cart, you can change the line that was creating a LineItem with no relationship to the cart:

    current_item = LineItem.new(:product_id => product_id, :price => product_price)
    

    to actually create a new line item in the current cart’s line_items collection:

    current_item = line_items.create(:product_id => product_id, :price => product_price)
    

    There were some other failures that you didn’t specifically ask about, but I looked into them anyways…

    Your products.yml does not specify an id field, so the product_id of 1 in line_items.yml does not end up referencing any product when you run the fixtures. You can hard-code id: 1, etc in your products.yml to address this.

    Next, there is just a typo in the CartsControllerTest — @cart_id isn’t defined, and should instead be @cart.id

    Finally, that unveils one other problem, which is actually a logic problem. Now your test to destroy a product fails, but this is actually because your logic does not allow you to destroy a product that has line_items (which your product does once we fixed the products.yml file to have ids specified). So… really the test is wrong and the code is right there, I suppose.

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

Sidebar

Related Questions

Howdy, codeboys and codegirls! I have came across a simple problem with seemingly easy
Howdy all. I am trying to solve a problem which is apparently not uncommon
Howdy guys, im having trouble finding help on creating a callback in certain situations.
Howdy, I have a DataRow pulled out of a DataTable from a DataSet. I
Howdy. Consider the following: SQL> DECLARE 2 b1 BOOLEAN; 3 b2 BOOLEAN; 4 FUNCTION
Howdy... here on my local LAN, I have a Windows Server 2k8 box with
What I want is, I think, relatively simple: > Bin = <<Hello.world.howdy?>>. > split(Bin,
Howdy and Thanks in Advance! I'm trying to access the USB port from a
Howdy! I am looking for a way to list all of the image files
Howdy, I'm writing a batch script that will run on a Windows XP machine.

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.