【问题标题】:Testing nested resource controller with rspec in Rails 3.1在 Rails 3.1 中使用 rspec 测试嵌套资源控制器
【发布时间】:2012-01-19 18:29:01
【问题描述】:

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

在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's 都是同一个错误。

我尝试从 routes.rb 文件中删除 :as =&gt; :following_subscriptions,但同样的问题。

我实际上已经拆分了 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)

编辑

哦,我的 rake 路线看起来像这样:

car_follow_subscriptions_da POST   /biler/:car_id/car_subscriptions(.:format)                     {:action=>"create", :controller=>"car_subscriptions", :locale=>"da"}

【问题讨论】:

    标签: ruby-on-rails rspec controller ruby-on-rails-3.1 routes


    【解决方案1】:

    根据rake routes 提供的内容,我猜你应该替换:

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

    与:

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

    【讨论】:

    • 是的!你是对的,这很奇怪,这应该在RubyOnRails guides 中有更好的记录!
    • 我想说你应该在你的应用程序控制器中设置一个默认的语言环境设置器
    • 在我的应用程序中,I18n.locale 是可用的,即使未在 ApplicationController 中设置,如链接中所述。意味着即使 URL 中未提供区域设置,应用程序也能正常工作,但测试失败...
    • 如果我将它添加到控制器,然后从 rspec 中删除它,它仍然会抛出 RoutingError
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    相关资源
    最近更新 更多