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

发布于 2024-12-08 01:17:37 字数 606 浏览 1 评论 0原文

也许我又只见树木不见森林了,但我似乎无法找到一种方法来编写一个功能测试来测试重定向到 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)

...。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昇り龍 2024-12-15 01:17:37

知道了。

通过测试的是:

  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

感谢以下帮助: 我该怎么办在 Rails 中测试时设置 HTTP_REFERER?

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?

碍人泪离人颜 2024-12-15 01:17:37

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文