Search code examples
ruby-on-railsrubyruby-on-rails-3testingfunctional-testing

Rails - Functional testing of redirect_to(request.referer)


Maybe I just missing the forest for the trees again, but I can't seem to figure out a way to write a functional test that tests a :destroy action that redirects to request.referer.

Code is:

  def destroy
    @step = Step.find(params[:id])
    @step.destroy

    respond_to do |format|
      format.html { redirect_to(request.referer) }
      format.xml  { head :ok }
    end
  end

Failing test is:

  test "should destroy step" do
    assert_difference('Step.count', -1) do
      delete :destroy, :id => @step.to_param
    end
    assert_redirected_to request.referer
  end

Having no luck using:

redirect_to(:back)

... as well.


Solution

  • Got it.

    Passing test is:

      test "should destroy step" do
        assert_difference('Step.count', -1) do
          @request.env['HTTP_REFERER'] = 'http://test.com/steps/1'
          delete :destroy, :id => @step.to_param
        end
        assert_redirected_to @request.env['HTTP_REFERER']
      end
    

    Thanks to help from: How do I set HTTP_REFERER when testing in Rails?