title: How do I pass a params parameter into an rspec controller test?
body: <p>I have the following rspec test:</p>
  
  <pre><code>def valid_attributes
    { "product_id" =&gt; "1" }
  end
  
  describe "POST create" do
    describe "with valid params" do
      it "creates a new LineItem" do
        expect {
          post :create, {:line_item =&gt; valid_attributes}, valid_session #my valid_session is blank
        }.to change(LineItem, :count).by(1)
      end
  </code></pre>
  
  <p>Which fails with this error:</p>
  
  <pre><code>1) LineItemsController POST create with valid params redirects to the created line_item
     Failure/Error: post :create, {:line_item =&gt; 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 &lt;top (required)&gt;'
  </code></pre>
  
  <p>This is my controller's create action:</p>
  
  <pre><code>def create
    @cart = current_cart
    product = Product.find(params[:product_id])
    @line_item = @cart.line_items.build(:product =&gt; 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
  </code></pre>
  
  <p>As you can see, my action expects a product_id from the request's <code>params</code> object.  <strong>How should I work this product_id into my rspec test?</strong></p>
  
  <p>I've tried placing this <code>before</code> statement:</p>
  
  <pre><code>before(:each) do
      ApplicationController.any_instance.stub(:product).and_return(@product = mock('product'))
    end
  </code></pre>
  
  <p>. . . but it changes nothing.  I am missing some rspec concept here somewhere.</p>