为什么RSpec的方法,“获取”,“发布”,“放置”,“删除”不在gem(或Rails外部)的控制器规范中工作?

我不是Rails或Rspec的新手,但我是制作gem的新手。 当我测试我的控制器时,REST方法“get”,“post”,“put”,“delete”给我一个未定义的方法错误。

您可以在下方找到代码,但如果您希望在贴图中看到它, 请单击此处 。

谢谢!

这是我的spec_helper:

$LOAD_PATH.unshift(File.dirname(__FILE__)) $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'rubygems' require 'active_support' unless defined? ActiveSupport # Need this so that mattr_accessor will work in Subscriber module require 'active_record/acts/subscribable' require 'active_record/acts/subscriber' require 'action_view' require 'action_controller' # Since we'll be testing subscriptions controller #require 'action_controller/test_process' require 'spec' require 'spec/autorun' # Need active_support to user mattr_accessor in Subscriber module, and to set the following inflection ActiveSupport::Inflector.inflections do |inflect| inflect.irregular 'dorkus', 'dorkuses' end require 'active_record' # Since we'll be testing a User model which will be available in the app # Tell active record to load the subscribable files ActiveRecord::Base.send(:include, ActiveRecord::Acts::Subscribable) ActiveRecord::Base.send(:include, ActiveRecord::Acts::Subscriber) require 'app/models/user' # The user model we expect in the application require 'app/models/person' require 'app/models/subscription' require 'app/models/dorkus' require 'app/controllers/subscriptions_controller' # The controller we're testing #... more but I think irrelevant 

我的subscriptions_spec:

 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe SubscriptionsController, "on GET index" do load_schema describe ", when only subscribable params are passed" do it "should list all the subscriptions of the subscribable object" end describe ", when only subscriber params are passed" do it "should list all the subscriptions of the subscriber" do u = User.create d1 = Dorkus.create d2 = Dorkus.create d1.subscribe! u d2.subscribe! u get :index, {:subscriber_type => "User", :subscriber_id => u.id} assigns[:subscriptions].should == u.subscriptions end end end 

我的订阅控制器:

 class SubscriptionsController < ActionController::Base def index end end 

错误:

 NoMethodError in 'SubscriptionsController on GET index , when only subscriber params are passed should list all the subscriptions of the subscriber' undefined method `get' for # /home/ramon/rails/acts_as_subscribable/spec/controllers/subscriptions_controller_spec.rb:21: 

因为这些方法不属于Rspec而属于Rails。

当您描述一个控制器时,它inheritance自Spec :: Rails :: Example :: ControllerExampleGroup ,它inheritance了FunctionalExampleGroup ,它inheritance了rails的ActionController :: TestCase。

如果你看一下ActionController :: TestCase的文档,你会发现这里定义了get / post / put / delete方法。

因此,如果您想在Rails之外访问这些方法,则需要重新定义它们。

添加:type => controller描述如下,对我有用

 describe TestController, :type => :controller do # ur stuff end 

上面接受的答案中的链接现在已被破坏。 这是更新后的链接(适用于2.3.8) http://api.rubyonrails.org/v2.3.8/classes/ActionController/TestCase.html

我正在使用我正在为Rails 2.3.8系统制作的gem这个问题。 然后我意识到我使用的是高于2.0的Rspec-Rails版本,当时我应该使用最新版本的Rspec-Rails-1.3(版本1.3.4)。

我卸载了所有RSpec-Rails v2相关的gems(rspec-core,rspec-expectations,rspec-mocks等),用v1.3.4配置了我的Gemfile,然后运行’bundle install’。

我正在使用配置为使用Bundler的spec / dummy中的虚拟Rails 2.3.8应用程序,以及使用与我的gems Gemfile(从gemspec文件中获取gems)相同的gem依赖项配置的Gemfile。

我运行’script / generate rspec’来为Rails生成RSpec文件/文件夹,我引用它来在我的gems Rakefile和spec_helper文件中进行适当的配置。