Rspec: Controller specs for 2 level nested resources

前端 未结 2 483
傲寒
傲寒 2020-12-17 09:28

my routes.rb

  namespace :magazine do
   resources :pages do
     resources :articles do
       resources :comments
     end
   end
  end

W

相关标签:
2条回答
  • 2020-12-17 10:05

    Your route requires at least two IDs: the comment's parent article, and the article's parent page.

    namespace :magazine do
      resources :pages do
        resources :articles do
          resources :comments
        end
      end
    end
    
    # => /magazine/pages/:page_id/articles/:article_id/comments
    

    All parent IDs must be provided for this route to work:

    it "renders the :index view" do
      get :index, {:page_id => @page.id, :article_id => @article.id}
      # [UPDATE] As of Rails 5, this becomes:
      # get :index, params: {:page_id => @page.id, :article_id => @article.id}
      response.should render_template("index")
    end
    
    0 讨论(0)
  • 2020-12-17 10:14

    With Rails 5 the params API changed:

    get :index, params: { page_id: @page.id, article_id: @article.id }
    

    https://relishapp.com/rspec/rspec-rails/v/3-7/docs/request-specs/request-spec#specify-managing-a-widget-with-rails-integration-methods

    0 讨论(0)
提交回复
热议问题