在routes.rb中,我有:
[some routes]
match '/me' => "scores#all_athlete_scores", :constraints => LoggedInChecker
devise_scope :user do
match '/me' => "devise/sessions#new"
end
match '/scores/athlete/:id', :to =>"scores#all_athlete_scores", :as => "all_athlete_scores"
[some more routes]
如果用户已登录,LoggedInChecker将返回true,如果不是,则返回false。如果为false,则第二条路由选择它并将用户发送到登录页面。
在scores_controller_test.rb中,我有:
def test_get_all_athlete_scores
[set up]
get :all_athlete_scores, :id => @user
end
奇怪的是,scores_controller_test中的request.filtered_parameters [:full_path]是/me?id=@user.id,而不是/ scores / athlete /:id'。
如果我将routes.rb更改为:
[some routes]
match '/scores/athlete/:id', :to =>"scores#all_athlete_scores", :as => "all_athlete_scores"
match '/me' => "scores#all_athlete_scores", :constraints => LoggedInChecker
devise_scope :user do
match '/me' => "devise/sessions#new"
end
[some more routes]
测试工作正常,request.filtered_parameters [:full_path] = / scores / athlete /:id。
完全被这个困惑 - /我在对all_athlete_scores进行GET时不应该匹配。
有什么想法吗?
答案 0 :(得分:2)
天钩,
我认为混淆的是你有一个方法和一个命名的路线' all_athlete_scores'。
get :all_athlete_scores, :id => @user
调用方法all_athlete_scores而不是命名路由,这正是您所期望的。
可能应该在此测试中测试该方法的功能,并将路由测试留给另一个测试。
希望有所帮助!