2010-03-30 52 views
20

我不是新來的Rails或Rspec,但我是新手製作寶石。當我測試我的控制器時,REST方法「get」,「post」,「put」,「delete」給我一個未定義的方法錯誤。爲什麼RSpec的方法「get」,「post」,「put」,「delete」在gem(或Rails之外)的控制器規範中工作?

下面你會發現代碼,但如果你喜歡看它在一個pastie,click here

謝謝!

這裏是我的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: 

回答

28

因爲這些方法不屬於Rspec,而是屬於Rails。

描述控制器時,它繼承自Spec::Rails::Example::ControllerExampleGroup,它繼承FunctionalExampleGroup,它繼承了rails的ActionController :: TestCase。

如果你看看​​,你會發現這是定義get/post/put/delete方法的地方。

所以,如果你想訪問Rails之外的這些方法,你需要重新定義它們。

+0

謝謝達米安。這是我正在尋找的答案。 :) – 2010-03-30 09:01:21

+11

你在哪裏以及如何「重新定義它們」? – botbot 2012-08-19 00:40:51

+0

方法是一種方法。所以你應該可以在任何模塊中重新定義它並將其包含在你的規格中 – 2015-06-19 07:10:57

9

添加:類型=>控制器來描述如下我

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

我會試試這個。但你在這裏回答我的其他問題:http://stackoverflow.com/questions/2577372/test-with-rspec-a-controller-outside-of-a-rails-environment。我會嘗試一下,如果它有效(我敢肯定它會),如果你決定在那裏添加你的答案,我會選擇你的。 :) – 2010-04-20 10:09:45

+0

應該是:type =>:控制器在rspec 2.11即 – huug 2012-10-19 08:15:59

+0

@huug - 謝謝,編輯。 – 2012-10-20 04:30:05

0

我遇到了這個問題,我正在爲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等),配置了我的Gemfile v1.3.4,並運行了'bundle install'。

我在配置爲使用Bundler的spec/dummy中配置了一個虛擬Rails 2.3.8應用程序,並使用與我的gem Gemfile(從gemspec文件中獲取gem)相同的gem依賴項配置了Gemfile。

我運行了'script/generate rspec'來爲Rails生成RSpec文件/文件夾,我引用它來在我的gem Rakefile和spec_helper文件中進行適當的配置。

相關問題