在 Rails 3.1 中使用 rspec 测试嵌套资源控制器
我正在尝试对嵌套资源的控制器进行测试。
嵌套在routes.rb中是这样的,
resources :cars, :only => [:index, :destroy, :show] do
resources :car_subscriptions, :only => [:new, :create], :as => :follow_subscriptions
end
我正在尝试最具体地测试创建操作:
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
end
it "redirects to the created car_subscription" do
post :create, :car_subscription => valid_attributes
response.should redirect_to(CarSubscription.last)
end
end
end
end
它实际上是由rails脚本生成的脚手架的一部分。我只修改了 valid_attributes 和第一个“it”中的帖子
,输出是这样的:
1) CarSubscriptionsController POST create with valid params creates a new CarSubscription
Failure/Error: post :create, :car_id => 1, :car_subscription => valid_attributes
ActionController::RoutingError:
No route matches {:car_id=>"1", :car_subscription=>{:car_id=>"1", :user_id=>"2"}, :controller=>"car_subscriptions", :action=>"create"}
# ./spec/controllers/car_subscriptions_controller_spec.rb:34:in `block (5 levels) in <top (required)>'
# ./spec/controllers/car_subscriptions_controller_spec.rb:33:in `block (4 levels) in <top (required)>'
对于所有“it”来说都是相同的错误。
我尝试删除 :as =>; :following_subscriptions
来自routes.rb 文件,但同样的问题。
我实际上已经拆分了 car_subscriptions 的资源,因此 index
和 destroy
不是嵌套的,而 create
和 new
嵌套在 :cars
中
我不想使用像这样的硬编码路径 answer但如果是的话唯一的方法,我可以尝试一下:
{ :post => "/forum_topics/1/forum_sub_topics" }.should route_to(:controller => "forum_sub_topics", :action => "create", :forum_topic_id => 1)
编辑
哦,我的耙子路线如下所示:
car_follow_subscriptions_da POST /biler/:car_id/car_subscriptions(.:format) {:action=>"create", :controller=>"car_subscriptions", :locale=>"da"}
I am trying to make a test for a controller for a nested resource.
The nesting is like this in the routes.rb
resources :cars, :only => [:index, :destroy, :show] do
resources :car_subscriptions, :only => [:new, :create], :as => :follow_subscriptions
end
I'm trying to test the create action most specifically:
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
end
it "redirects to the created car_subscription" do
post :create, :car_subscription => valid_attributes
response.should redirect_to(CarSubscription.last)
end
end
end
end
It's actually a part of the scaffold generated by rails script. And I only modified the valid_attributes and the post in the first 'it'
And the output is this:
1) CarSubscriptionsController POST create with valid params creates a new CarSubscription
Failure/Error: post :create, :car_id => 1, :car_subscription => valid_attributes
ActionController::RoutingError:
No route matches {:car_id=>"1", :car_subscription=>{:car_id=>"1", :user_id=>"2"}, :controller=>"car_subscriptions", :action=>"create"}
# ./spec/controllers/car_subscriptions_controller_spec.rb:34:in `block (5 levels) in <top (required)>'
# ./spec/controllers/car_subscriptions_controller_spec.rb:33:in `block (4 levels) in <top (required)>'
It's the same error for all 'it's.
I've tried removing the :as => :following_subscriptions
from the routes.rb file but the same problem.
I have actually split up the resources of car_subscriptions so index
and destroy
are in not nested, and create
and new
are nested in :cars
I don't want to use hard coded paths like in this answer but if it is the only way, I can give it a try:
{ :post => "/forum_topics/1/forum_sub_topics" }.should route_to(:controller => "forum_sub_topics", :action => "create", :forum_topic_id => 1)
EDIT
Oh, and my rake routes looks like this:
car_follow_subscriptions_da POST /biler/:car_id/car_subscriptions(.:format) {:action=>"create", :controller=>"car_subscriptions", :locale=>"da"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从
rake paths
提供的内容来看,我想你应该将:替换为:
From what
rake routes
provides, I guess you should replace:with: