Ruby-on-rails Rspec - Rails - 如何跟踪重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2865378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:41:21  来源:igfitidea点击:

Rspec - Rails - How to follow a redirect

ruby-on-railstestingrspecweb-testing

提问by Jonas S?derstr?m

Does anyone know how to make rspec follow a redirect (in a controller spec)? (e.g test/unit has follow_redirect!)

有谁知道如何使 rspec 遵循重定向(在控制器规范中)?(例如测试/单元有 follow_redirect!)

I have tried "follow_redirect!" and "follow_redirect" but only get

我试过“follow_redirect!” 和“follow_redirect”但只得到

undefined method `follow_redirect!' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_1:0xb6df5294>

For example:
When I create an account the page is redirected to accounts page and my new account should be at the top of the list.

例如:
当我创建一个帐户时,页面被重定向到帐户页面,我的新帐户应该在列表的顶部。

it "should create an account" do
  post :create, :name => "My New Account"
  FOLLOW_REDIRECT!
  response.code.should == "200"
  accounts = assigns[:accounts]
  accounts[0].name.should == "My New Account"
end

But FOLLOW_REDIRECT! needs to be changed to something that actually works.

但是FOLLOW_REDIRECT!需要更改为实际工作的东西。

采纳答案by andersjanmyr

If you want to test the redirect you are moving outside of the rspec-rails domain.

如果您想测试重定向,您将移出 rspec-rails 域。

You can use Webrat or some other integration-test framework to test this.

您可以使用 Webrat 或其他一些集成测试框架来测试它。

The easiest way to solve this without resorting to integration testing is probably to mock out the method that is causing the redirect.

在不诉诸集成测试的情况下解决此问题的最简单方法可能是模拟导致重定向的方法。

回答by zetetic

I think this is the default behavior for rspec-railscontroller tests, in the sense that you can set an expectation on the response status and/or path, and test for success.

我认为这是rspec-rails控制器测试的默认行为,从某种意义上说,您可以对响应状态和/或路径设置期望,并测试是否成功。

For example:

例如:

it "should create an account" do
  post :create
  response.code.should == "302"
  response.should redirect_to(accounts_path)
end

回答by toonsend

You can access the redirect location with

您可以访问重定向位置

response.headers['Location']

you could then request that directly.

然后你可以直接请求。

回答by Carlos Esteban Lopez Jaramillo

The spec is out of scope, if you want to follow a redirect use request spec, the equivalent of integration test in Test::Unit.

该规范超出范围,如果您想遵循重定向使用请求规范,相当于 Test::Unit 中的集成测试。

In request specs follow_redirect!works as well as in Test::Unit.

在请求规范follow_redirect!中和 Test::Unit 中一样有效。

Or if you want to redirect inmediately use _via_redirectas suffix for the verb, example:

或者,如果您想立即重定向,请_via_redirect用作动词的后缀,例如:

post_via_redirect :create, user: @user

回答by Foton

Try to use integration/request tests. They are using web-like acces through routing to controllers. For example: I have for Rails 2 app in file /spec/integration/fps_spec.rb

尝试使用集成/请求测试。他们通过路由到控制器来使用类似网络的访问。例如:我在文件中有 Rails 2 应用程序/spec/integration/fps_spec.rb

 require 'spec_helper'

 describe "FinPoradci" do 

   it "POST /fps.html with params" do
     fp_params={:accord_id => "FP99998", :under_acc => "OM001", :first_name => "Pavel", :last_name => "Novy"}
     fp_test=FinPoradce.new(fp_params)
     #after create follow redirection to show
     post_via_redirect "/fps", {:fp => fp_params}
     response.response_code.should == 200 # => :found ,  not 302 :created
     new_fp=assigns(:fp)
     new_fp.should_not be_empty
     new_fp.errors.should be_empty
     flash[:error].should be_empty
     flash[:notice].should_not be_empty
     response.should render_template(:show)
   end
 end

and it works. Until you want to send headers (for basic http authorization).

它有效。直到您想发送标头(用于基本的 http 授权)。

 env={'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user,password)}
 post_via_redirect "/fps", {:fp => fp_params}, env

is fine for create, but after redirection it returns 401 and needs new authorization. SO I have to split it in 2 tests: creation and show on result of creation.

可以创建,但重定向后返回 401 并需要新的授权。所以我必须将它分成 2 个测试:创建和显示创建结果。

回答by Vjatseslav Gedrovits

For RSpec / Capybara + Rails

对于 RSpec / Capybara + Rails

response_headers['Location']

But it works only if there is no delay before redirect. If it is there, then it's harder to follow the logic.

但它只有在重定向前没有延迟时才有效。如果它在那里,那么就更难遵循逻辑。