2016-01-02 82 views
0

當我運行一個簡單的測試時出現這個錯誤get :index。下面是測試代碼:沒有路線匹配{:controller =>「提交」}

class SubmissionsControllerTest < ActionController::TestCase 
    setup do 
    @submission = submissions(:one) 
    end 

    test "should get index" do 
    get :index 
    assert_response :redirect 
    # assert_not_nil assigns(:submissions) 
    end 

我已經建立了這個控制器的路線,雖然在我的routes.rb文件:

resources :courses do 
    resources :assignments do 
     get 'export', :controller => 'assignments', :action => 'export' 
     resources :memberships 
     resources :submissions do 
     resources :evaluations do 
      delete 'destroy', controller: 'reviews', action: 'destroy' 
     end 
     end 

     resources :questions do 
     resources :responses 
     resources :scales 
     end 

     resources :reviews do 
     get :assign_reviews, :on => :collection 
     post :edit_review, :on => :collection 
     end 

    end 
    end 

而且,這是我得到的路線意見書當我運行耙路線:

course_assignment_submissions GET /courses/:course_id/assignments/:assignment_id/submissions(.:format) submissions#index 
POST /courses/:course_id/assignments/:assignment_id/submissions(.:format) submissions#create 
new_course_assignment_submission GET /courses/:course_id/assignments/:assignment_id/submissions/new(.:format) submissions#new 
edit_course_assignment_submission GET /courses/:course_id/assignments/:assignment_id/submissions/:id/edit(.:format) submissions#edit 
course_assignment_submission GET /courses/:course_id/assignments/:assignment_id/submissions/:id(.:format) submissions#show 
PUT /courses/:course_id/assignments/:assignment_id/submissions/:id(.:format) submissions#update 
DELETE /courses/:course_id/assignments/:assignment_id/submissions/:id(.:format) submissions#destroy 

這裏是我的索引操作代碼:

def index 
    if !current_user.instructor?(@course) 
     raise CanCan::AccessDenied.new("Not authorized!") 
    end 

    registrations = @course.registrations 
    @students = registrations.select{|r| !r.user.pseudo or @assignment.memberships.any?{|m| m.pseudo_user_id == r.user_id } }.map{|r| r.user } 
    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @students } 
    end 
    end 

有誰知道這個錯誤可能來自哪裏?

+0

您可以將您的測試代碼? –

+0

剛剛添加了我的測試代碼 – Kevin

+0

您能否包含您的測試代碼的類(例如,class SubmissionsController

回答

0

因爲你submissions控制器嵌套到assignmentsassignmentscourses你有一個:assignment_id:course_id傳遞到您的GET請求。

因此,例如:

get :index, course_id: your_course, assignment_id: your_assignment 
+0

我試過這個,它給了我一個錯誤ActionController :: RoutingError:沒有路由匹配{:assignment_id =>「1」,:controller =>「提交」}。此外,我不認爲這是問題,因爲我的會員資格路線是以相同的方式定義的,並且該控制器的get:index測試通過 – Kevin

+0

我已更新答案。好的,這聽起來很奇怪,因爲你的路線必須以某種方式獲得ID。 – Tobias

相關問題