title: mocha stubs with exception - rspec test not passing
body: <p>Hey,
  I am trying to use Mocha and Rspec to test a scenario where a method always raises some exception. </p>
  
  <p>Here is the controller code I am trying to test:</p>
  
  <pre><code>  def add_parent
      begin
        parent = Entity.find_by_id(params[:parent_id])
        if !parent.nil?
          @entity.add_parent!(parent)
          flash[:success] = "Entity successfully updated."
        else
          raise "Parent does not exist."
        end
      rescue
        flash[:error] = "Something bad happened. #{$!}"
      end
      redirect_to @entity
    end
  </code></pre>
  
  <p>Here is the test code:</p>
  
  <pre><code>it "should flash error if exception is thrown when adding parent" do
        Entity.any_instance.stubs(:add_parent!).raises(Exception)
        lambda do
          post :add_parent, :id =&gt; @entity[:id], 
            :parent_id =&gt; @parent_entity[:id]
        end.should_not change(@entity.parents, :count)
        flash[:error].should =~ /something bad happened/i
      end
  </code></pre>
  
  <p>Here is the method which is being stubbed:</p>
  
  <pre><code>  def add_parent!(parent)
      Entity.transaction do
        lock!('lock in share mode')
        self.parents &lt;&lt; parent
      end
    end
  </code></pre>
  
  <p>I am getting the following rspec error, which is pretty uninformative so I don't know how to resolve it..</p>
  
  <pre><code>Failures:
  
    1) EntitiesController POST 'add_parent' for signed-in users allow access with edit permission should flash error if exception is thrown when adding parent
       Failure/Error: post :add_parent, :id =&gt; @entity[:id],
       Exception
       # ./app/controllers/entities_controller.rb:81:in `add_parent'
       # ./spec/controllers/entities_controller_spec.rb:1010
  </code></pre>