Rspec:2级嵌套资源的控制器规格 [英] Rspec: Controller specs for 2 level nested resources

查看:161
本文介绍了Rspec:2级嵌套资源的控制器规格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的routes.rb

my routes.rb

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

在写控制器规范的注释:

While writing controller specs for Comments:

describe "GET 'index'" do
    before(:each) do
     @user = FactoryGirl.create(:user)
     @page = FactoryGirl.build(:page)
     @page.creator = @user
     @page.save
     @article = FactoryGirl.create(:article)
     @comment_attributes = FactoryGirl.attributes_for(:comment, :article_id => @article )
   end
it "populates an array of materials" do
  get :index, ??
  #response.should be_success
  assigns(:comments)
end

it "renders the :index view" do
  get :index, ?? 
  response.should render_template("index")
end

end 


b $ b

任何想法如何给页面和文章引用get:index?
如果我给:get:index,:article_id => @ article.id

我得到的错误如下:

Any idea how to give the page and article reference to get :index ?? if I give : get :index, :article_id => @article.id
Error I get is below:

 Failure/Error: get :index, :article_id => @article.id
 ActionController::RoutingError:
   No route matches {:article_id =>"3", :controller=>"magazine/comments"}


推荐答案

您的路由需要至少两个ID:注释的父文章和文章的父页面。 / p>

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

/ strong>必须为此路线提供工作:

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}
  response.should render_template("index")
end

这篇关于Rspec:2级嵌套资源的控制器规格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆