在 Rails 3.1 中使用 rspec 测试嵌套资源控制器

发布于 2024-12-21 04:55:38 字数 2672 浏览 0 评论 0原文

我正在尝试对嵌套资源的控制器进行测试。

嵌套在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 的资源,因此 indexdestroy 不是嵌套的,而 createnew嵌套在 :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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最终幸福 2024-12-28 04:55:38

rake paths提供的内容来看,我想你应该将:替换

 post :create, :car_id => 1, :car_subscription => valid_attributes

为:

 post :create, :car_id => 1, :car_subscription => valid_attributes, :locale => "da"

From what rake routes provides, I guess you should replace:

 post :create, :car_id => 1, :car_subscription => valid_attributes

with:

 post :create, :car_id => 1, :car_subscription => valid_attributes, :locale => "da"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文