22

I'm not new to Rails or Rspec, but I'm new to making gems. When I test my controllers, the REST methods "get", "post", "put", "delete" give me an undefined method error.

Below you'll find code, but if you prefer to see it in a pastie, click here.

Thanks!

Here's my 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 

My 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 

My subscriptions controller:

 class SubscriptionsController < ActionController::Base def index end end 

The error:

 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: 

4 Answers 4

30

Because these methods don't belong to Rspec but to Rails.

When you describe a controller, it inherits from Spec::Rails::Example::ControllerExampleGroup, which inherits FunctionalExampleGroup, which inherits the rails' ActionController::TestCase.

If you look at ActionController::TestCase's documentation you'll find that's where the get/post/put/delete methods are defined.

So if you want to get access to these methods outside of Rails, you need to redefine them.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Damien. This is the answer I was looking for. :)
where and how do you "redefine them"?
A method is a method. So you should be able to redefine it in any module and include it in your specs
9

Adding :type => controller to describe as follows worked for me

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

2 Comments

I'll try this out. But you answered my other question here: stackoverflow.com/questions/2577372/…. I'll try it out then if it works (I'm pretty sure it will), I'll go pick yours, if you decide to add your answer there. :)
should be :type => :controller in rspec 2.11 that is
5

The link from the accepted answer above is now broken. Here's the updated link (for 2.3.8) http://api.rubyonrails.org/v2.3.8/classes/ActionController/TestCase.html

Comments

0

I was having this issue with a gem I'm making for a Rails 2.3.8 system. I then realized that I was using an Rspec-Rails version above 2.0, when I should be using the latest version of Rspec-Rails-1.3 (version 1.3.4).

I uninstalled all RSpec-Rails v2 related gems (rspec-core, rspec-expectations, rspec-mocks, etc), configured my Gemfile with v1.3.4, and ran 'bundle install'.

I'm using a dummy Rails 2.3.8 app in spec/dummy configured to use Bundler, and with a Gemfile configured with the same gem dependencies as my gems Gemfile (which grabs the gems from the gemspec file).

I ran 'script/generate rspec' to generate the RSpec files/folders for Rails, which I referenced to make the appropriate configurations in my gems Rakefile and spec_helper file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.