简体   繁体   English

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

[英]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. 也许我只是再次错过树林,但我似乎无法找到一种方法来编写一个功能测试,测试一个:destroy动作重定向到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? 感谢以下方面的帮助: 如何在Rails中进行测试时设置HTTP_REFERER?

you write an integration test and use one of delete_via_redirect which should follow the redirect. 你编写了一个集成测试,并使用了一个应该遵循重定向的delete_via_redirect。 You may also have to set the the HTTP_REFERER header - see guide.rubyonrails.org 您可能还必须设置HTTP_REFERER标头 - 请参阅guide.rubyonrails.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM