title: How to write rspec controller spec model_mock that accounts for no mass assignment
body: <p>I'm using rspec to test a controller that uses a model that has attr_accessible declared. I don't want to be testing that attr_accesible is working (my model spec does that), but I do want to make sure my controller isn't doing a mass assignment.</p>
  
  <p>Specifically, I have a model like so:</p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
    attr_accessible :body
    validates :body,    :presence =&gt; true,
    validates :user,    :presence =&gt; true
    belongs_to :user
  end
  </code></pre>
  
  <p>and a slightly tweaked generated controller (xml format lines removed):</p>
  
  <pre><code>  def create
      # this line keeps the rspec test mock happy
      @post = Post.new(params[:post].merge(:user =&gt; current_user))
  
      # Below are the correct lines of code for runtime for attr_accessible
      # @post = Post.new(params[:post])
      # @post.user = current_user            
  
      respond_to do |format|
        if @post.save
          format.html { redirect_to(@post, :notice =&gt; 'Post was successfully created.') }
        else
          format.html { render :action =&gt; "new" }
        end
      end
    end
  </code></pre>
  
  <p>I have a rspec test with a mock controller that passes with the above:</p>
  
  <pre><code>describe "POST create" do
    describe "with valid params" do
      it "assigns a newly created post as @post" do
        Post.stub(:new).
          with({'body' =&gt; 'hi there', 'user' =&gt; @user}) { mock_post(:save =&gt; true) }
        post :create, :post =&gt; {'body' =&gt; 'hi there'}
        assigns(:post).should be(mock_post)
      end
    end
  end
  </code></pre>
  
  <p>What I want to do is change the rspec test so that will correctly validate the controller when I comment out the first @post line and uncomment the following two @post lines. That way, I'll be validating that the controller will work work correctly with the real model, but I can continue to use mocks for testing. I've tried several approaches and seem to be going around in circles (and yes, I'm a newbie to Rails and Rspect :p)</p>
  
  <p>Many thanks in advance!</p>