Rails 3.1:测试“删除”“销毁””在控制器和路线规格中
我正在测试一个使用 Sorcery 进行身份验证的应用程序。我有一个会话控制器,用于处理用户登录/注销以及与操作一起进行的测试。据我所知,销毁操作通常需要一个 id 作为参数,但对于注销功能来说这是不必要的。
上面的
resources :sessions
# match "/signout", :to => "sessions#destroy"
两个
describe "DELETE 'destroy'" do
it "should log the user out" do
login_user(Factory(:user))
delete :destroy
controller.current_user.should be_nil
controller.should_not be_signed_in
end
end
测试
it "should route DELETE /sessions to sessions#destroy" do
{ :delete => "/sessions" }.should route_to(
:controller => "sessions",
:action => "destroy"
)
end
都会失败,因为路由需要一个 id。有没有办法摆脱这种必要性?我知道我可以使用名为“signout_path”的路由,但我只是好奇是否仍然可以使用 session_path, :method => :delete 而不传递 id。
真正令我震惊的是,如果我取消注释控制器规范通过的匹配“/signout”(但是,路由规范没有)。匹配线如何导致控制器规范通过?
I'm testing an app that uses Sorcery for authentication. I have a sessions controller which handles user signin / signout with tests to go along with the actions. I understand that a destroy action usually takes an id as a parameter, but it's unnecessary with a signout feature.
routes.rb
resources :sessions
# match "/signout", :to => "sessions#destroy"
sessions_controller_spec.rb
describe "DELETE 'destroy'" do
it "should log the user out" do
login_user(Factory(:user))
delete :destroy
controller.current_user.should be_nil
controller.should_not be_signed_in
end
end
session_routes_spec.rb
it "should route DELETE /sessions to sessions#destroy" do
{ :delete => "/sessions" }.should route_to(
:controller => "sessions",
:action => "destroy"
)
end
Both of the above tests fail because the route expects an id. Is there a way to get rid of this necessity? I know I could just use the named "signout_path" route, but I am just curious if I can still use session_path, :method => :delete without passing it an id.
What's really shocking to me is if I uncomment the match "/signout" the controller spec passes (however, the route spec does not). How does the match line cause the controller spec to pass?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在 config/routes.rb 中将销毁操作映射为:会话资源的收集方法:
默认情况下,销毁是一种“成员”方法,需要指定的资源才能对其进行操作。
Try to map the destroy action as :collection method of sessions resources, in your config/routes.rb:
By default destroy is an "member" method that requires a resource specified to work on it.