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 8467631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:42:03+00:00 2026-06-10T15:42:03+00:00

I have a test that creates the following error: 1) Failure: test_should_get_create(ProductRequestsControllerTest) [/Users/noahc/Dropbox/mavens/test/functional/product_requests_controller_test.rb:37]: ProductRequest.count

  • 0

I have a test that creates the following error:

1) Failure:
test_should_get_create(ProductRequestsControllerTest) [/Users/noahc/Dropbox/mavens/test/functional/product_requests_controller_test.rb:37]:
"ProductRequest.count" didn't change by 1.
<2> expected but was
<1>.

How do I trouble shoot this? Specifically, how can I get a more specific detailed error?

Here is my test:

 test "should get create" do
    sign_in(FactoryGirl.create(:user))
     assert_difference('ProductRequest.count') do
      post :create, product_request: FactoryGirl.attributes_for(:product_request)
    end

    assert_response :success
  end

and here is my controller:

 def create
  cart = current_cart
  rows = CartRow.find_all_by_cart_id(cart.id)

  rows.each do |row|
    product_request = ProductRequest.new(params[:product_request])
    product_request.user_id = current_user.id
    product_request.product_id = row.product_id
    product_request.quantity = row.quantity
    product_request.save
  end

  redirect_to root_path 
 end

I believe the issue is that I don’t have a cart defined. How do I create a cart that unit::test can see? I’ve tried using FactoryGirl to create a cart, but that didn’t seem to work.

carts_factory.rb

FactoryGirl.define do

  factory :cart do
  end
end

Updated test:

test "should get create" do
    sign_in(FactoryGirl.create(:user))
    user = FactoryGirl.create(:user)
    product = FactoryGirl.create(:product)
     assert_difference('ProductRequest.count') do
      post :create, product_request: FactoryGirl.attributes_for(:product_request, user: user.id, product: product.id)
    end

    assert_response :success
  end

and current_cart

 def current_cart 
      Cart.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound 
      cart = Cart.create 
      session[:cart_id] = cart.id
      cart
    end

Second Update

I’ve updated the factories as you’ve suggested.

Here is what my test now looks like:

 test "should get create" do
  user = FactoryGirl.create(:user)
  cart = FactoryGirl.create(:cart_with_1_row)
  product = FactoryGirl.create(:product)
  sign_in(user)

  product = FactoryGirl.create(:product)
  assert_difference('ProductRequest.count') do
    post :create, { product_request: FactoryGirl.attributes_for(:product_request, user_id: user.id, product_id: product.id, cart_id: cart.id) }
  end

  assert_response :success
end

Here’s it in a test console:

irb(main):016:0> a = { product_request: FactoryGirl.attributes_for(:product_request, user_id: user.id, product_id: product.id, cart_id: cart.id) }
=> {:product_request=>{:quantity=>10, :street=>"123 street", :city=>"Some City", :state=>"Iowa", :zip=>"13829", :user_id=>1, :product_id=>2, :cart_id=>1}}
  • 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-10T15:42:05+00:00Added an answer on June 10, 2026 at 3:42 pm

    First of all, CartRow.find_all_by_cart_id(cart.id), this is not good design. Much better when you ask Cart model for its row, for example: rows = cart.rows

    I think the issue is that you don’t have rows inside you cart.

    As I see you store cart id in session but when you call controller in test you does not provide session. You need create cart and cart’s rows and when store cart_id in session before you call controller. And it is important to merge current session and session with cart_id. For example:

    test "should get create" do
      user = FactoryGirl.create(:user)
      cart = FactoryGirl.create(:cart_with_1_row)
      sign_in(user)
    
      product = FactoryGirl.create(:product)
      assert_difference('ProductRequest.count') do
        post :create, { product_request: FactoryGirl.attributes_for(:product_request, user: user.id, product: product.id) }, { cart_id: cart.id }.merge(session)
      end
    
      assert_response :success
    end
    

    Also you need update your cart and cart’s row factories:

    FactoryGirl.define do
      factory :cart do
        factory :cart_with_1_row do
          after(:create) do |cart|
            FactoryGirl.create(:cart_row, cart: cart)
          end
        end
      end
    
      factory :cart_row do
        cart
      end
    end
    

    I think your CartRow model looks like:

    class CartRow < ActiveRecord::Base
      belongs_to :cart
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a unit test that creates a mock calls my method to be
I have a method CreateAccount(...) that I want to unit test. Basically it creates
We have a load test that runs of 100 concurrent users. We also have
I have the following request rspec test: describe CRUD do it should list users
I have the following code in an external JS file (ie: test.js); which creates
I have the following problem. I got a bat file that runs testcomplete test.
I have a set of test accounts that are going to be created but
I have a test that is successful (among other things) if a certain callback
I have a unit test that fails because headers are already sent. However, the
I have a unit test that I am writing and have run into an

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.