Rspec - Rails - 如何遵循重定向
有谁知道如何使 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为这是 rspec-rails 控制器测试的默认行为,在感觉您可以对响应状态和/或路径设置期望,并测试是否成功。
例如:
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:
您可以访问重定向位置,
然后可以直接请求该位置。
You can access the redirect location with
you could then request that directly.
如果您想测试重定向,请移至 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.
如果您想遵循重定向使用请求规范(相当于 Test::Unit 中的集成测试),则该规范超出范围。
在请求规范中,
follow_redirect!
与 Test::Unit 中的效果一样好。或者,如果您想立即重定向,请使用
_via_redirect
作为动词的后缀,例如: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:尝试使用集成/请求测试。他们通过路由到控制器来使用类似网络的访问。
例如:
我在文件
/spec/integration/fps_spec.rb
中有 Rails 2 应用程序,它可以工作。直到您想要发送标头(用于基本的 http 授权)。
对于 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
and it works. Until you want to send headers (for basic http authorization).
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.
对于 RSpec / Capybara + Rails
但只有在重定向之前没有延迟的情况下它才有效。
如果存在,那么就很难遵循逻辑。
For RSpec / Capybara + Rails
But it works only if there is no delay before redirect.
If it is there, then it's harder to follow the logic.