title: How do I stub a method to raise an error using Ruby MiniTest?
body: <p>I'm trying to test a Rails controller branch that is triggered when the model method raises an error.</p>
  
  <pre><code>def my_controller_method
    @my_object = MyObject.find(params[:id])
  
    begin
      result = @my_object.my_model_method(params)
    rescue Exceptions::CustomError =&gt; e
      flash.now[:error] = e.message       
      redirect_to my_object_path(@my_object) and return
    end
  
    # ... rest irrelevant
  end
  </code></pre>
  
  <p>How can I get a Minitest stub to raise this Error?</p>
  
  <pre><code>it 'should show redirect on custom error' do
    my_object = FactoryGirl.create(:my_object)
  
    # stub my_model_method to raise Exceptions::CustomError here
  
    post :my_controller_method, :id =&gt; my_object.to_param
    assert_response :redirect
    assert_redirected_to my_object_path(my_object)
    flash[:error].wont_be_nil
  end
  </code></pre>