简体   繁体   English

如何使用rspec测试带有嵌套路由的控制器

[英]How to test controllers with nested routes using rspec

I create controller with InherritedResource 我用InherritedResource创建控制器

class AppsController < InheritedResources::Base
  belongs_to :company

  # Devise
  before_filter :login_or_oauth_required
  # CanCan
  load_and_authorize_resource
end

and try to test it with Rspec by this method 并尝试通过此方法用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

The result is 结果是

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

How to tell Rspec to "Get" in to my nested route 如何告诉Rspec“进入”我的嵌套路线

My routes 我的路线

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

I try to follow this Question How to test controllers with nested routes using Rspec? 我尝试遵循此问题如何使用Rspec使用嵌套路由测试控制器? but it's not work on my case 但这对我的情况不起作用

I use Rails 3.1.1 and rspac 2.7 我使用Rails 3.1.1和rspac 2.7

I found solution :-P 我找到了解决方案:-P

What I did was just to change from 我所做的只是改变

get :index

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM