Rspec - Rails - 如何遵循重定向

发布于 2024-09-01 18:41:41 字数 590 浏览 2 评论 0原文

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

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

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

例如:
当我创建帐户时,页面将重定向到帐户页面,并且我的新帐户应位于列表顶部。

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

但是FOLLOW_REDIRECT!需要改变为真正有效的东西。

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

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

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.

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

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

发布评论

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

评论(6

诗酒趁年少 2024-09-08 18:41:41

我认为这是 rspec-rails 控制器测试的默认行为,在感觉您可以对响应状态和/或路径设置期望,并测试是否成功。

例如:

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

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

For example:

it "should create an account" do
  post :create
  response.code.should == "302"
  response.should redirect_to(accounts_path)
end
南巷近海 2024-09-08 18:41:41

您可以访问重定向位置,

response.headers['Location']

然后可以直接请求该位置。

You can access the redirect location with

response.headers['Location']

you could then request that directly.

骑趴 2024-09-08 18:41:41

如果您想测试重定向,请移至 rspec-rails 域之外。

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

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

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

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

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

醉梦枕江山 2024-09-08 18:41:41

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

在请求规范中,follow_redirect! 与 Test::Unit 中的效果一样好。

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

post_via_redirect :create, user: @user

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

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

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

post_via_redirect :create, user: @user
别闹i 2024-09-08 18:41:41

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

 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

,它可以工作。直到您想要发送标头(用于基本的 http 授权)。

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

对于 create 来说很好,但是重定向后它返回 401 并且需要新的授权。
所以我必须将其分为两个测试:创建和显示创建结果。

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

 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).

 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.

好菇凉咱不稀罕他 2024-09-08 18:41:41

对于 RSpec / Capybara + Rails

response_headers['Location']

但只有在重定向之前没有延迟的情况下它才有效。
如果存在,那么就很难遵循逻辑。

For 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.

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