Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing member routes of a resources in rails3 with rspec

I'm creating an application that will allow the user to take an exam. The exam object has_many questions and I want the user to take the exam in two parts. In order to implement this workflow, I've 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... I have all of this working and now I'm trying to write tests for it - I know that's backwards but it took me a while to figure out complex forms and stuff. I want to test that you cannot access the exam controller unless you are a signed in user. This is straight forward for new and create but I'm having trouble figuring out how to test the nested members. Here's what I've 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!

like image 398
spinlock Avatar asked Mar 24 '11 21:03

spinlock


1 Answers

Try that one:

get :get_part1, :id => @exam   (or @exam.id)
like image 193
Markus Proske Avatar answered Sep 21 '22 06:09

Markus Proske