开发者

Rails - Functional testing of redirect_to(request.referer)

开发者 https://www.devze.com 2023-04-09 21:46 出处:网络
Ma开发者_如何学Pythonybe 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.

Ma开发者_如何学Pythonybe 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.


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?


you write an integration test and use one of delete_via_redirect which should follow the redirect. You may also have to set the the HTTP_REFERER header - see guide.rubyonrails.org

0

精彩评论

暂无评论...
验证码 换一张
取 消