title: What is going on here: rspec stub(:new).with...?
body: <p>I'm a little confused about what is going on with the scaffold controller specs that rspec generates.  It seemed to be making sense until I added authorization to my app and now I need to update my tests.</p>
  
  <pre><code>MyClass.stub(:new).with('these' =&gt; 'params') { mock_my_class(:save =&gt; true) }
  </code></pre>
  
  <p>In my controller I merge a hash into params when creating a new record (it needs the current_user id to be valid).  MyClass.new(params[:my_class].merge(:user_id => current_user.id))</p>
  
  <p>Test Fails</p>
  
  <pre><code>expected: ({"these"=&gt;"params"})
  got: ({"these"=&gt;"params", "user_id"=&gt;315})
  </code></pre>
  
  <p>It makes sense that the test fails because the new method receives params it didn't expect. It expected to receive {'these' => 'params'} but it actually received {'these' => 'params', 'user_id' => 1234}</p>
  
  <p>So my natural reaction is to adjust the test because the new method <em>should</em> receive {'these' => 'params', 'user_id' => 1234} and return the mock object.  </p>
  
  <p>So I add to the test as follows:</p>
  
  <pre><code>   MyClass.stub(:new).with({'these' =&gt; 'params', 'user_id' =&gt; @user.id}) { mock_my_class(:save =&gt; true) }   
  </code></pre>
  
  <p>Here is where I get thrown through a loop.  The output of the test is as follows:</p>
  
  <pre><code>expected: ({"these"=&gt;"params", "user_id"=&gt;298})
  got: ({"these"=&gt;"params"})
  </code></pre>
  
  <p>It seems as if a successful test is magically evading me.  I'm sure there is a logical reason for these results, but I can't seem to figure them out.</p>
  
  <p>Any help? :)</p>
  
  <p>note:</p>
  
  <p>The rspec site says the following:</p>
  
  <pre><code>Account.should_receive(:find).with("37").and_return(account)
  </code></pre>
  
  <p>or</p>
  
  <pre><code>Account.stub!(:find).and_return(account)
  </code></pre>
  
  <p>This is easy enough to follow it just seems odd the the scaffold generated would not contain these methods (unless I botched something which is possible (:  )</p>
  
  <hr>
  
  <p>Passes</p>
  
  <pre><code>login_admin
  describe "with valid params" do
    it "assigns a newly created forum_sub_topic as @forum_sub_topic" do
      ForumSubTopic.stub(:new) { mock_forum_sub_topic(:save =&gt; true) }
      ForumSubTopic.should_receive(:new).with({"these"=&gt;"params", "user_id"=&gt; @admin.id})  #PASS!
      post :create, :forum_sub_topic =&gt; {'these' =&gt; 'params'}
      assigns(:forum_sub_topic).should be(mock_forum_sub_topic) #PASS!
    end
  end
  </code></pre>
  
  <hr>
  
  <p>Fails</p>
  
  <pre><code>login_admin
  describe "with valid params" do
    it "assigns a newly created forum_sub_topic as @forum_sub_topic" do
      ForumSubTopic.stub(:new).with({'these' =&gt; 'params', 'user_id' =&gt; @user.id}) { mock_forum_sub_topic(:save =&gt; true) } 
      post :create, :forum_sub_topic =&gt; {'these' =&gt; 'params'}
      assigns(:forum_sub_topic).should be(mock_forum_sub_topic)
    end
  end
  </code></pre>