Rails - redirect_to的功能测试(request.referer)

时间:2011-10-03 05:55:22

标签: ruby-on-rails ruby ruby-on-rails-3 testing functional-testing

也许我只是再次错过树林,但我似乎无法找到一种方法来编写一个功能测试来测试:重定向到request.referer的destroy动作。

代码是:

  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

测试失败是:

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

没有运气使用:

redirect_to(:back)

......以及。

2 个答案:

答案 0 :(得分:4)

知道了。

通过测试是:

  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

感谢您提供的帮助:How do I set HTTP_REFERER when testing in Rails?

答案 1 :(得分:0)

你编写了一个集成测试并使用了delete_via_redirect,它应该遵循重定向。您可能还需要设置HTTP_REFERER标头 - 请参阅guide.rubyonrails.org