如何使用 rspec 测试带有嵌套路由的控制器 [英] How to test controllers with nested routes using rspec
本文介绍了如何使用 rspec 测试带有嵌套路由的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 InherritedResource 创建控制器
I create controller with InherritedResource
class AppsController < InheritedResources::Base
belongs_to :company
# Devise
before_filter :login_or_oauth_required
# CanCan
load_and_authorize_resource
end
并尝试通过这种方法使用Rspec进行测试
and try to test it with Rspec by this method
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 进入"我的嵌套路由
How to tell Rspec to "Get" in to my nested route
我的路线
resources :companies do
resources :apps do
resources :shelves do
resources :publications
end
end
end
我试着按照这个问题 如何测试控制器使用 Rspec 嵌套路由? 但它不适用于我的情况
I try to follow this Question How to test controllers with nested routes using Rspec? but it's not work on my case
我使用 Rails 3.1.1 和 rspac 2.7
I use Rails 3.1.1 and rspac 2.7
推荐答案
我找到了解决方案 :-P
I found solution :-P
我所做的只是为了改变
get :index
到
get :index, :company_id => @company_1.id
这篇关于如何使用 rspec 测试带有嵌套路由的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文