Rails 3.1:测试“删除”“销毁””在控制器和路线规格中

发布于 2024-12-08 11:39:46 字数 882 浏览 0 评论 0原文

我正在测试一个使用 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 技术交流群。

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

发布评论

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

评论(1

拥抱影子 2024-12-15 11:39:46

尝试在 config/routes.rb 中将销毁操作映射为:会话资源的收集方法:

resources :sessions, :collection => [:destroy]

默认情况下,销毁是一种“成员”方法,需要指定的资源才能对其进行操作。

Try to map the destroy action as :collection method of sessions resources, in your config/routes.rb:

resources :sessions, :collection => [:destroy]

By default destroy is an "member" method that requires a resource specified to work on it.

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