我是Ruby新手,我必须为Rails应用程序编写测试。
我的问题是我不知道如何调用accept
FriendshipsController
操作
以下是 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
在下面的测试代码中,我看到调用了FriendshipsController.create
方法:
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
但是如何调用 FriendshipsController.accept
方法?
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
put
调用无异常,但永远不会调用accept
FriendshipsController
。
这是 routes.rb
RedmineApp::Application.routes.draw do
# ...
resources :users do
# ...
resources :friendships do
# ...
member do
put :accept
put :deny
end
end
end
# ...
end
调用accept
的正确方法是什么?
修改
以下是路线
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
答案 0 :(得分:1)
尝试使用$ 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
输出告诉我们accept_friendship
没有采用user_id
参数。
put :accept, :id => friendship_from_current_to_friend.id
答案 1 :(得分:0)
我发现了问题: 每个友谊请求都包含两个相应的友谊对象
在任何一个
friend_user
,接受错误的友情ID 或current_user
,接受正确的友情ID accept
方法将不调用。
因此,如果合适的用户接受正确的友情ID,则接受方法将被称为罚款。但这并没有说明为什么不能通过错误的输入调用accept方法。