我不是新來的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:
謝謝達米安。這是我正在尋找的答案。 :) – 2010-03-30 09:01:21
你在哪裏以及如何「重新定義它們」? – botbot 2012-08-19 00:40:51
方法是一種方法。所以你應該可以在任何模塊中重新定義它並將其包含在你的規格中 – 2015-06-19 07:10:57