ruby-on-rails-3 – 我如何通过路由通配测试控制器
发布时间:2020-12-17 01:58:45 所属栏目:百科 来源:网络整理
导读:为了获得问卷清单,我使用了 GET "/questionnaires/user/1/public/true/mine/true/shared/true" 在routes.rb我有 /questionnaires/*myparams(.:format) {:controller="questionnaires",:action="list"} 控制器使用route globbing在list方法中创建查询 class Q
为了获得问卷清单,我使用了
GET "/questionnaires/user/1/public/true/mine/true/shared/true" 在routes.rb我有 /questionnaires/*myparams(.:format) {:controller=>"questionnaires",:action=>"list"} 控制器使用route globbing在list方法中创建查询 class QuestionnairesController < ApplicationController before_filter :authenticate def list myparams = params[:myparams].split("/").to_h end # ... end 我正在尝试为spec文件中的所有选项创建测试用例 describe "GET list" do it "returns the list of questionnaires for the user" do get :list # ... end end 我运行rspec时得到的是 Failures: 1) QuestionnairesController List GET list returns the list of questionnaires for the user Failure/Error: get :list No route matches {:controller=>"questionnaires",:action=>"list"} # ./spec/controllers/questionnaires_controller_spec.rb:20 问题是如何编写spec文件以将globbed参数传递给rspec.我喜欢做这样的事情: describe "GET list" do it "returns the list of questionnaires for the user" do get :list,"/user/1/public/true/mine/true/shared/true" end end 并更改参数以测试不同的情况 解决方法
globbing发生在调度程序中,因此在调用控制器时已经分配了params.当达到控制器动作时,全局参数应该已经分成params [:myparams]中的数组.
如果要在控制器中测试它,只需按照调度程序的方式设置params散列: describe "GET 'list'" do it "should be successful" do get :list,:myparams => "user/1/public/true/mine/true/shared/true".split("/") response.should be_success end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |