简体   繁体   中英

Call Member Routes in ActionController::TestCase

I'am new to Ruby and I have to write tests for a Rails Application.

My problem is that I don't know how can I invoke the accept action of the FriendshipsController

Here is the FriendshipsController :

class FriendshipsController < ApplicationSocialController
  # ...

  # create method is getting called
  def create
    # accessing params[:user_id]
  end

  # accept method doesn't get called
  def accept
    # accessing params[:id] and params[:user_id]
  end
end

In the following test code I saw that the FriendshipsController.create method is called:

current_user = users(:users_002)
friend_user = users(:users_003)
@request.session[:user_id] = current_user.id
post :create, :user_id => friend_user.id # works fine

But how can I call the FriendshipsController.accept method?

current_user = users(:users_002)
friend_user = users(:users_003)
friendship_from_current_to_friend = create_friendship(current_user, friend_user)
# friend_user should can accept friendship request
@request.session[:user_id] = friend_user.id
put :accept, :user_id => current_user.id, :id => friendship_from_current_to_friend.id

The put call works without an exception, but accept of FriendshipsController is never called.

Here is the routes.rb

RedmineApp::Application.routes.draw do
  # ...
  resources :users do
    # ...
    resources :friendships do
      # ...
      member do
        put :accept
        put :deny
      end
    end
  end
  # ...
end

What is the correct way to invoke accept ?

Edit

Here is an except of routes

GET    /users/:user_id/friendships/accepted(.:format)       friendships#accepted
GET    /users/:user_id/friendships/pending(.:format)        friendships#pending
GET    /users/:user_id/friendships/denied(.:format)         friendships#denied
GET    /users/:user_id/friendships/write_message(.:format)  friendships#write_message
PUT    /users/:user_id/friendships/:id/accept(.:format)     friendships#accept
PUT    /users/:user_id/friendships/:id/deny(.:format)       friendships#deny
GET    /users/:user_id/friendships(.:format)                friendships#index
POST   /users/:user_id/friendships(.:format)                friendships#create
GET    /users/:user_id/friendships/new(.:format)            friendships#new
GET    /users/:user_id/friendships/:id/edit(.:format)       friendships#edit
GET    /users/:user_id/friendships/:id(.:format)            friendships#show
PUT    /users/:user_id/friendships/:id(.:format)            friendships#update
DELETE /users/:user_id/friendships/:id(.:format)            friendships#destroy

Try using $ rake routes

           Prefix Verb   URI Pattern                       Controller#Action
      friendships GET    /friendships(.:format)            friendships#index
                  POST   /friendships(.:format)            friendships#create
   new_friendship GET    /friendships/new(.:format)        friendships#new
  edit_friendship GET    /friendships/:id/edit(.:format)   friendships#edit
       friendship GET    /friendships/:id(.:format)        friendships#show
                  PATCH  /friendships/:id(.:format)        friendships#update
                  PUT    /friendships/:id(.:format)        friendships#update
                  DELETE /friendships/:id(.:format)        friendships#destroy
accept_friendship PUT    /friendships/:id/accept(.:format) friendships#accept
  deny_friendship PUT    /friendships/:id/deny(.:format)   friendships#deny
                  GET    /friendships(.:format)            friendships#index
                  POST   /friendships(.:format)            friendships#create
                  GET    /friendships/new(.:format)        friendships#new
                  GET    /friendships/:id/edit(.:format)   friendships#edit
                  GET    /friendships/:id(.:format)        friendships#show
                  PATCH  /friendships/:id(.:format)        friendships#update
                  PUT    /friendships/:id(.:format)        friendships#update
                  DELETE /friendships/:id(.:format)        friendships#destroy
            users GET    /users(.:format)                  users#index
                  POST   /users(.:format)                  users#create
         new_user GET    /users/new(.:format)              users#new
        edit_user GET    /users/:id/edit(.:format)         users#edit
             user GET    /users/:id(.:format)              users#show
                  PATCH  /users/:id(.:format)              users#update
                  PUT    /users/:id(.:format)              users#update
                  DELETE /users/:id(.:format)              users#destroy

The output tells us that accept_friendship does not take a user_id param.

put :accept, :id => friendship_from_current_to_friend.id

I found the problem: Every friendship request consists of two corresponding friendship objects

On either

  • the right user , ie friend_user , accepts the wrong friendship id or
  • the wrong user , ie current_user , accepts the correct friendship id

the accept method will not called.

So if the right user accepts the right friendship id, the accept method will called fine. But this doesn't clarify why the accept method doesn't get called with a wrong input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM