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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:25:13+00:00 2026-06-15T13:25:13+00:00

I have the following rspec test: def valid_attributes { product_id => 1 } end

  • 0

I have the following rspec test:

def valid_attributes
  { "product_id" => "1" }
end

describe "POST create" do
  describe "with valid params" do
    it "creates a new LineItem" do
      expect {
        post :create, {:line_item => valid_attributes}, valid_session #my valid_session is blank
      }.to change(LineItem, :count).by(1)
    end

Which fails with this error:

1) LineItemsController POST create with valid params redirects to the created line_item
   Failure/Error: post :create, {:line_item => valid_attributes}, valid_session
   ActiveRecord::RecordNotFound:
     Couldn't find Product without an ID
     # ./app/controllers/line_items_controller.rb:44:in `create'
     # ./spec/controllers/line_items_controller_spec.rb:87:in `block (4 levels) in <top (required)>'

This is my controller’s create action:

def create
  @cart = current_cart
  product = Product.find(params[:product_id])
  @line_item = @cart.line_items.build(:product => product)

  respond_to do |format|
    if @line_item.save
      format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' }
      format.json { render json: @line_item.cart, status: :created, location: @line_item }
    else
      format.html { render action: "new" }
      format.json { render json: @line_item.errors, status: :unprocessable_entity }
    end
  end
end

As you can see, my action expects a product_id from the request’s params object. How should I work this product_id into my rspec test?

I’ve tried placing this before statement:

before(:each) do
    ApplicationController.any_instance.stub(:product).and_return(@product = mock('product'))
  end

. . . but it changes nothing. I am missing some rspec concept here somewhere.

  • 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-15T13:25:15+00:00Added an answer on June 15, 2026 at 1:25 pm

    I ended up resolving my issue by using a fixture instead of attempting to mock the solution as suggested in another answer.

    The reason for this is that the controller does the query to get information from the database: product = Product.find(params[:product_id]) and I found a fixture-based solution was quicker to resolve my problem than one using a mock and I could not figure out how to stub the query quickly (the fixtures also help with another test on the controller so it eventually helped anyway.

    For reference:

    I referenced my fixture with this line toward the top of the test: fixtures :products

    I changed my test to:

    describe "POST create" do
      describe "with valid params" do
        it "creates a new LineItem" do
          expect {
              post :create, :product_id => products(:one).id
           }.to change(LineItem, :count).by(1)
        end
    

    And here is my fixture file, products.yml:

    one:
        name: FirstProduct
        price: 1.23
    
    two:
        name: SecondProduct
        price: 4.56
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following test written in RSpec: describe '#create' do ... it 'redirects
I have the following request rspec test: describe CRUD do it should list users
I have the following rspec: require 'spec_helper' describe KnowledgesController do before (:each) do @knowledge
I have the following RSpec example which is passing: describe UsersController do it should
I have the following Rspec test: it Password reset instructions do user = Factory(:user)
I have the following in my controller: def create @board = Board.find(session[:board]) @greeting =
Still a bit new to rspec and can't get the following test to pass
I have written following test in Rspec: expect { ... }.to change( User.where(:profile.exists =>
Running rails 3.2.3 with guard/spork/rspec/factory_girl and have the following in my spec helper: Spork.prefork
I have following nested objects. I am using @Valid for validation in my controller.

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.