ruby-on-railsrubyrspecrspec-railsspecs

RSpec Routing Error w/ Nested Resources


I have a spec that I feel should be passing without issue, but I believe that since it is a nested resource, it may be throwing off my request. I am using Rails 4.2 with Ruby 2.3.3. What's going on? I know this is a valid route, because when it appears when I hit rake routes

routes.rb

    scope '/organizations/:organization_id' do
      get 'dashboard', to: 'projects#dashboard'
      resources :projects, except: [:delete] do
        get 'configure', to: 'projects#configure'
        post 'configure', to: 'projects#configure'
        get 'team', to: 'projects#team'
        get 'subprojects', to: 'projects#subprojects'
        collection do
          get 'search', to: 'projects#search'
          get 'find', to: 'projects#find'
          post 'create_by_sf_id', to: 'projects#create_by_sf_id'
        end
        resources :courses do
          get 'module_progress', to: 'courses#module_progress'
          get 'add_content', to: 'courses#add_content'
          get 'summary', to: 'courses#summary'
          post 'summary', to: 'courses#summary'
        end
        resources :tasks
      end
    end

projects_controller_spec.rb

describe 'GET project_team' do
  it 'should render the project team page' do
    get :team, organization_id: organization.id, id: project.id
    expect(response.code).to eq '200'
  end
end

projects_controller.rb

def team
  @team = @project.project_team
end

...Aaaand the resulting error:

1) ProjectsController when authenticating as a customer GET project_team should render the project team page
 Failure/Error: get :team, organization_id: organization.id, id: project.id

 ActionController::UrlGenerationError:
   No route matches {:action=>"team", :controller=>"projects", :id=>"460", :organization_id=>"417"}
 # ./spec/controllers/projects_controller_spec.rb:90:in `block (4 levels) in <top (required)>'

Solution

  • You have team nested under projects which is giving you:

    project_team GET  /organizations/:organization_id/projects/:project_id/team(.:format)  projects#team
    

    So this:

    get :team, organization_id: organization.id, id: project.id
    

    Should probably be this:

    get :team, organization_id: organization.id, project_id: project.id
    

    It also seems like you might be heading for trouble here:

    def team
      @team = @project.project_team
    end
    

    Because you don't look up @project (unless you do it in a before_action hook).

    Finally, I would be tempted to create a ProjectTeamController. That way, you could use a show method instead of the non-standard team method. But, that's a matter of personal preference.