Testing resource member routes in rails3 with rspec

I am creating an application that will allow the user to pass the exam. The exam object has_many questions, and I want the user to pass the exam in two parts. To implement this workflow, I created the following routes in config / routes.rb:

resources :exam do member do get 'get_part1' put 'put_part1' get 'get_part2' put 'put_part2' end end 

So, when the user issues GET / exam /: id / get_part1, they are shown the first set of questions, etc. .... It all works for me, and now I'm trying to write tests for it - I know what is back, but it took me some time to figure out complex shapes and stuff. I want to verify that you cannot access the exam controller if you are not a subscribed user. This is straightforward for new and creation, but it's hard for me to figure out how to test nested members. Here is what I have tried so far:

 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 

However, this test fails with the following error:

 Failure/Error: get get_part1_exam_path(@exam) ActionController::RoutingError: No route matches {:controller=>"exams", :action=>"/exams/1/get_part1"} 

Any help would be greatly appreciated. Thanks!

+8
ruby-on-rails-3 routing rspec
source share
1 answer

Try the following:

 get :get_part1, :id => @exam (or @exam.id) 
+13
source share

All Articles