Ruby on Rails - Rspec - 控制器测试 - 嵌套路由

时间:2014-03-17 23:22:53

标签: ruby-on-rails ruby-on-rails-3 rspec

我得到了一个非常基本的控制器测试

require 'spec_helper'

describe Admin::OrdersController do 

    describe "GET #order_detail" do 
        before :each do 
            new_admin = FactoryGirl.create(:admin)
            sign_in new_admin
            @storefront = FactoryGirl.create(:storefront)
            @order = FactoryGirl.create(:order)

         end

         it "assigns the requested order to @order" do
             get :order_detail, { :storefront_id => @storefront.id, :order_id => @order.id }
             assigns(:order).should eq(@order)
         end

         it "renders the :show template" do
             get :order_detail, {:storefront_id => @storefront.id, :order_id => @order.id}
             response.should render_template :order_detail
         end
    end 
end

这两个操作都会出现以下错误:

ActionController::RoutingError:
    No route matches {:storefront_id=>"14", :order_id=>"1", :controller=>"admin/orders", :action=>"order_detail"}

来自routes.rb:

resources :storefronts do
  resources :orders do
    member do
      get :order_detail
    end
  end
end

我想

get :order_detail, { :storefront_id => @storefront.id, :order_id => @order.id }

是生成路线的正确方法,但不幸的是,它不是。

1 个答案:

答案 0 :(得分:0)

您可以在应用程序根目录中使用rake routesbundle exec rake routes查看Rails生成的路由。我在一个全新的rails应用程序中进行了相同的资源设置,并且rake路由输出如下(仅适用于订单明细路径):

order_detail_storefront_order GET      /storefronts/:storefront_id/orders/:id/order_detail(.:format)                   orders#order_detail

如您所见,rails期待:id而非:order_id。尝试将:order_id更改为:id作为规范中的参数键。