开发者

Rails 3.1: testing "DELETE 'destroy'" in controller and route specs

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 开发者_C百科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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜