簡體   English   中英

帶有嵌套資源的RSpec路由錯誤

[英]RSpec Routing Error w/ Nested Resources

我有一個規格,我認為應該沒有問題,但我認為,由於它是嵌套資源,因此可能會放棄我的要求。 我正在將Rails 4.2與Ruby 2.3.3一起使用。 這是怎么回事? 我知道這是一條有效路線,因為當我打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

... Aaa和導致的錯誤:

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)>'

您有嵌套在projects下的team ,這使您:

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

所以這:

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

應該可能是這樣的:

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

似乎您在這里可能還會遇到麻煩:

def team
  @team = @project.project_team
end

因為您不查找@project (除非您在before_action掛鈎中進行此操作)。

最后,我很想創建一個ProjectTeamController 這樣,您可以使用show方法代替非標准的team方法。 但是,這是個人喜好問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM