2011-11-01 194 views
10

創建控制器InherritedResource如何使用測試用嵌套路由控制器rspec的

class AppsController < InheritedResources::Base 
    belongs_to :company 

    # Devise 
    before_filter :login_or_oauth_required 
    # CanCan 
    load_and_authorize_resource 
end 

,並嘗試通過該方法與Rspec的測試它

require "spec_helper" 

include Devise::TestHelpers 

describe AppsController do 
    before(:each) do 
    @company_1 = Factory.build(:company) 
    application_1 = Factory.create(:application, :company => @company_1) 
    application_2 = Factory.create(:application, :company => @company_1) 
    application_3 = Factory.create(:application, :company => @company_1)  
    @company_2 = Factory.build(:company) 

    @user_1 = Factory.create(:user) 
    role_1 = Factory.create(:publisher_role, :company => @company_1) 
    profile_1 = Factory.create(:profile, :company => @company_1, :user => @user_1, :roles => [role_1]) 
    end 

    describe "index action" do 
    it "user_1 should have 3 applications from company_1" do 
     sign_in @user_1 
     params = {"company_id"=>"1"} 
     get :index 
     assigns[:apps].should have(3).items 
    end 
    end 
end 

結果是

Failure/Error: get :index 
    ActionController::RoutingError: 
     No route matches {:controller=>"apps"} 

如何告訴Rspec「獲取」我的嵌套路線

我的路線

resources :companies do 
    resources :apps do 
     resources :shelves do 
     resources :publications 
     end 
    end 
    end 

我儘量遵循這一問題How to test controllers with nested routes using Rspec?但它不是我的情況下

我使用的Rails 3.1.1和2.7 rspac

+1

FWIW,Rails的導遊勸阻嵌套資源超出一平「的資源不應該被嵌套超過10層深,」(http://guides.rubyonrails.org/routing.html#nested-resources )向讀者提及這個[monstrosity](http://weblog.jamisbuck.org/2007/2/5/nesting-resources)。但你做watcha必須做。 – brntsllvn

回答

39

我找到了解決方案的工作:-P

我所做的只是從

get :index 

get :index, :company_id => @company_1.id 
+0

或在新的rspec語法'get:index,params:{company_id:@ company_1.id}' –

相關問題