我的routes.rb
namespace :magazine do
resources :pages do
resources :articles do
resources :comments
end
end
end
Run Code Online (Sandbox Code Playgroud)
在为评论编写控制器规范时:
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
Run Code Online (Sandbox Code Playgroud)
任何想法如何给页面和文章引用get:index ?? 如果我给:get:index,:article_id => @ article.id
我得到的错误如下:
Failure/Error: get :index, …Run Code Online (Sandbox Code Playgroud) 我正在尝试为嵌套资源的控制器进行测试.
在routes.rb中嵌套是这样的
resources :cars, :only => [:index, :destroy, :show] do
resources :car_subscriptions, :only => [:new, :create], :as => :follow_subscriptions
end
Run Code Online (Sandbox Code Playgroud)
我正在尝试最具体地测试创建操作:
describe CarSubscriptionsController do
def valid_attributes
{:car_id => '1', :user_id => '2'}
end
describe "POST create" do
describe "with valid params" do
it "creates a new CarSubscription" do
expect {
post :create, :car_id => 1, :car_subscription => valid_attributes
}.to change(CarSubscription, :count).by(1)
end
it "assigns a newly created car_subscription as @car_subscription" do
post :create, :car_subscription => valid_attributes
assigns(:car_subscription).should be_a(CarSubscription)
assigns(:car_subscription).should be_persisted …Run Code Online (Sandbox Code Playgroud)