我正在创建一个允许用户参加考试的应用程序。考试对象有很多问题,我希望用户分两部分参加考试。为了实现此工作流程,我在config/routes.rb中创建了以下路由:
resources :exam do
member do
get 'get_part1'
put 'put_part1'
get 'get_part2'
put 'put_part2'
end
end
因此,当用户发出GET/exam/:id/get_part1时,将向他们显示第一组问题,以此类推...我已经完成了所有这些工作,现在我正尝试为其编写测试-我知道这是倒退的,但是我花了一段时间才弄清楚复杂的形式和内容。我要测试,除非您是登录用户,否则您将无法访问考试 Controller 。对于新建和创建,这很简单,但是我很难弄清楚如何测试嵌套成员。到目前为止,这是我尝试过的方法:
before(:each) do
@exam = Exam.create
end
it "should deny access to 'get_part1'" do
get get_part1_exam_path(@exam)
response.should redirect_to(signin_path)
end
但是,此测试失败,并出现以下错误:
Failure/Error: get get_part1_exam_path(@exam)
ActionController::RoutingError:
No route matches {:controller=>"exams", :action=>"/exams/1/get_part1"}
任何帮助将不胜感激。谢谢!
最佳答案
试试那个:
get :get_part1, :id => @exam (or @exam.id)
关于ruby-on-rails-3 - 使用rspec在Rails中测试资源的成员路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5426022/