Rspec 测试redirect_to :back

发布于 2024-11-08 03:07:54 字数 335 浏览 0 评论 0原文

如何在 rspec 中测试 redirect_to :back

我明白了

ActionController::RedirectBackError:
此操作的请求中未设置 HTTP_REFERER,因此无法成功调用 redirect_to :back。如果这是测试,请确保指定 request.env["HTTP_REFERER"]

如何在测试中设置 HTTP_REFERER

How do you test redirect_to :back in rspec?

I get

ActionController::RedirectBackError:
No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env["HTTP_REFERER"].

How do I go about setting the HTTP_REFERER in my test?

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

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

发布评论

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

评论(6

友谊不毕业 2024-11-15 03:07:54

使用 RSpec,您可以在 before 块中设置引用者。当我尝试在测试中直接设置引用者时,无论我把它放在哪里似乎都不起作用,但是 before 块可以解决问题。

describe BackController < ApplicationController do
  before(:each) do
    request.env["HTTP_REFERER"] = "where_i_came_from"
  end

  describe "GET /goback" do
    it "redirects back to the referring page" do
      get 'goback'
      response.should redirect_to "where_i_came_from"
    end
  end
end

Using RSpec, you can set the referer in a before block. When I tried to set the referer directly in the test, it didn't seem to work no matter where I put it, but the before block does the trick.

describe BackController < ApplicationController do
  before(:each) do
    request.env["HTTP_REFERER"] = "where_i_came_from"
  end

  describe "GET /goback" do
    it "redirects back to the referring page" do
      get 'goback'
      response.should redirect_to "where_i_came_from"
    end
  end
end
把昨日还给我 2024-11-15 03:07:54

如果有人偶然发现这一点并且他们正在使用 request 规范,则您需要在您发出的请求上显式设置标头。测试请求的格式取决于您使用的 RSpec 版本以及是否可以使用关键字参数而不是位置参数。

let(:headers){ { "HTTP_REFERER" => "/widgets" } }

it "redirects back to widgets" do 
  post "/widgets", params: {}, headers: headers # keyword (better)
  post "/widgets", {}, headers                  # positional

  expect(response).to redirect_to(widgets_path)
end

https://relishapp.com/rspec/rspec-rails/docs /请求规范/请求规范

If someone stumbles upon this and they're using request specs, you'll need to explicitly set the headers on the request you're making. The format of the test request depends on which version of RSpec you're using and if you can use keyword arguments instead of positional arguments.

let(:headers){ { "HTTP_REFERER" => "/widgets" } }

it "redirects back to widgets" do 
  post "/widgets", params: {}, headers: headers # keyword (better)
  post "/widgets", {}, headers                  # positional

  expect(response).to redirect_to(widgets_path)
end

https://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec

撧情箌佬 2024-11-15 03:07:54

来自 rails 指南 当使用新的请求样式请求请求时:

describe BackController < ApplicationController do
  describe "GET /goback" do
    it "redirects back to the referring page" do
      get :show, 
        params: { id: 12 },
        headers: { "HTTP_REFERER" => "http://example.com/home" }
      expect(response).to redirect_to("http://example.com/home")
    end
  end
end

From the rails guide when requesting the request with the new request style:

describe BackController < ApplicationController do
  describe "GET /goback" do
    it "redirects back to the referring page" do
      get :show, 
        params: { id: 12 },
        headers: { "HTTP_REFERER" => "http://example.com/home" }
      expect(response).to redirect_to("http://example.com/home")
    end
  end
end
香橙ぽ 2024-11-15 03:07:54

恕我直言,接受的答案有点hacky。更好的替代方案是将 HTTP_REFERER 设置为应用程序中的实际 url,然后期望重定向回来:

describe BackController, type: :controller do
  before(:each) do
    request.env['HTTP_REFERER'] = root_url
  end

  it 'redirects back' do
    get :whatever
    response.should redirect_to :back
  end
end
  • 重定向到随机字符串常量感觉好像它是偶然发生的
  • 您利用了 rspec 的内置功能在功能中准确表达您想要的内容
  • 您不会引入和重复魔术字符串值

对于较新版本的 rspec,您可以使用期望来代替:

expect(response).to redirect_to :back

IMHO the accepted answer is a bit hacky. A better alternative would be to set the HTTP_REFERER to an actual url in your application and then expect to be redirected back:

describe BackController, type: :controller do
  before(:each) do
    request.env['HTTP_REFERER'] = root_url
  end

  it 'redirects back' do
    get :whatever
    response.should redirect_to :back
  end
end
  • Redirecting to a random string constant feels as if it works by accident
  • You take advantage of rspec's built in functionality to express exactly what you wanted
  • You don't introduce and repeat magic string values

For newer versions of rspec, you can use expectations instead:

expect(response).to redirect_to :back
守望孤独 2024-11-15 03:07:54

关于在集成测试中测试 :back 链接,我首先访问一个死端页面,我认为该页面不太可能被用作链接,然后访问我正在测试的页面。所以我的代码看起来像这样

   before(:each) do
       visit deadend_path
       visit testpage_path
    end
    it "testpage Page should have a Back button going :back" do
      response.should have_selector("a",:href => deadend_path,
                                        :content => "Back")
    end

但是这确实有一个缺陷,如果链接确实是到 deadend_path,那么测试将错误地通过。

In regard to testing :back links in integration tests, I first visit a deadend page, that I think is not likely to be ever used as a link, and then the page I am testing. So my code looks like this

   before(:each) do
       visit deadend_path
       visit testpage_path
    end
    it "testpage Page should have a Back button going :back" do
      response.should have_selector("a",:href => deadend_path,
                                        :content => "Back")
    end

However this does have the flaw that if the link is really to the deadend_path, then the test will incorrectly pass.

橘味果▽酱 2024-11-15 03:07:54
request.env['HTTP_REFERER'] = '/your_referring_url'
request.env['HTTP_REFERER'] = '/your_referring_url'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文