【发布时间】:2023-03-10 18:52:01
【问题描述】:
要获取问卷列表,我使用
GET "/questionnaires/user/1/public/true/mine/true/shared/true"
在 routes.rb 我有
/questionnaires/*myparams(.:format) {:controller=>"questionnaires", :action=>"list"}
控制器使用路由通配符在列表方法中创建查询
class QuestionnairesController < ApplicationController
before_filter :authenticate
def list
myparams = params[:myparams].split("/").to_h
end
# ...
end
我正在尝试为规范文件中的所有选项创建测试用例
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
问题是如何编写规范文件以将全局参数传递给 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
并更改参数以测试不同的情况
【问题讨论】:
标签: ruby-on-rails-3 rspec2 glob