[rspec-users] undefined method `route_for

48 views
Skip to first unread message

Amit Kulkarni

unread,
Nov 10, 2009, 7:44:27 AM11/10/09
to rspec...@rubyforge.org
Hi all,
I am writing scenarios for testing my routes but it is giving me error
as undefined method `route_for and also for params_from.
Did i miss something which i need to add in my controller

My code is as follows:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe HomeController, "routes" do
describe "route generation" do
it "should map { :controller => 'home', :action => 'index' } to
/home" do
route_for(:controller => 'home', :action => 'index').should ==
'/home'
end

it "should map { :controller => 'home' } RESTfully" do
params_from( :get, '/home' ).should == { :controller => 'home',
:action => 'index' }
end
end
end
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

David Chelimsky

unread,
Nov 10, 2009, 9:59:25 AM11/10/09
to rspec...@rubyforge.org
On Tue, Nov 10, 2009 at 4:44 AM, Amit Kulkarni <li...@ruby-forum.com> wrote:
Hi all,
I am writing scenarios for testing my routes but it is giving me error
as undefined method `route_for and also for params_from.
Did i miss something which i need to add in my controller

My code is as follows:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe HomeController, "routes" do
 describe "route generation" do
   it "should map { :controller => 'home', :action => 'index' } to
/home" do
     route_for(:controller => 'home', :action => 'index').should ==
'/home'
   end

   it "should map { :controller => 'home' } RESTfully" do
     params_from( :get, '/home' ).should == { :controller => 'home',
:action => 'index' }
   end
 end
end

route_to, route_for and params_from are all available in controller specs, which are identified by:

1. spec/controllers in their path (i.e. spec/controllers/foo_controller_spec

2. describe "route generation", :type => :controller do

If neither of those cases is true, then you won't have access to the right helpers and matchers.

HTH,
David

Amit Kulkarni

unread,
Nov 11, 2009, 4:42:47 AM11/11/09
to rspec...@rubyforge.org
Thanks David for the information but still i am getting the same error.
What i have done is:
I have home controller.
now i am writing route scenarios in home_controller_spec.rb which is
under spec/controllers directory

I tried your code which looks like as

describe "route generation", :type => :HomeController do


it "should map { :controller => 'home', :action => 'index' } to
/home" do
route_for(:controller => 'home', :action => 'index').should ==
'/home'
end

it "should map { :controller => 'home' } RESTfully" do
params_from( :get, '/home' ).should == { :controller => 'home',
:action => 'index' }
end
end

But it is not working,i know i am missing something.

Also can you explain the first point?
Can you tell me up what i need to add to get it running atleast :-)

David Chelimsky

unread,
Nov 11, 2009, 7:46:52 AM11/11/09
to rspec...@rubyforge.org
On Wed, Nov 11, 2009 at 1:42 AM, Amit Kulkarni <li...@ruby-forum.com> wrote:
Thanks David for the information but still i am getting the same error.
What i have done is:
I have home controller.
now i am writing route scenarios in home_controller_spec.rb which is
under spec/controllers directory

I tried your code which looks like as

describe "route generation", :type => :HomeController do
   it "should map { :controller => 'home', :action => 'index' } to
/home" do
     route_for(:controller => 'home', :action => 'index').should ==
'/home'
   end

   it "should map { :controller => 'home' } RESTfully" do
     params_from( :get, '/home' ).should == { :controller => 'home',
:action => 'index' }
   end
 end

But it is not working,i know i am missing something.

Also can you explain the first point?
Can you tell me up what i need to add to get it running atleast :-)

If the spec is in spec/controllers/home_controller_spec.rb, you don't need the :type argument, so just get rid of that and you should be fine. If this still doesn't work, please post the command you are using as well.

For future reference, if you put a controller or routing spec in another directory, then you do need the :type argument, but :type means the type of spec, not the type of controller:

describe "route generation", :type => :controller do

HTH,
David

Amit Kulkarni

unread,
Nov 11, 2009, 8:58:51 AM11/11/09
to rspec...@rubyforge.org
Hi David still no success.

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe HomeController, "routes" do
describe "route generation" do

it "should map { :controller => 'home', :action => 'index' } to /home"
do
route_for(:controller => 'home', :action => 'index').should ==
'/home'
end

it "should map { :controller => 'home' } RESTfully" do
params_from( :get, '/home' ).should == { :controller => 'home',
:action => 'index' }
end
end

end

I am using "spec home_controller_spec.rb" by going under
spec/controllers/ directory.

David Chelimsky

unread,
Nov 11, 2009, 10:43:06 AM11/11/09
to rspec...@rubyforge.org
On Nov 11, 2009, at 5:58 AM, Amit Kulkarni <li...@ruby-forum.com> wrote:

> Hi David still no success.
> require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
> describe HomeController, "routes" do
> describe "route generation" do
> it "should map { :controller => 'home', :action => 'index' } to /
> home"
> do
> route_for(:controller => 'home', :action => 'index').should ==
> '/home'
> end
>
> it "should map { :controller => 'home' } RESTfully" do
> params_from( :get, '/home' ).should == { :controller => 'home',
> :action => 'index' }
> end
> end
> end
>
> I am using "spec home_controller_spec.rb" by going under
> spec/controllers/ directory.

Don't do that :)

Go to the project root directory and say "spec spec/controllers/
home_controller_spec.rb"

Amit Kulkarni

unread,
Nov 12, 2009, 2:19:46 AM11/12/09
to rspec...@rubyforge.org
David Chelimsky wrote:
> On Nov 11, 2009, at 5:58 AM, Amit Kulkarni <li...@ruby-forum.com> wrote:
>
>>
>> it "should map { :controller => 'home' } RESTfully" do
>> params_from( :get, '/home' ).should == { :controller => 'home',
>> :action => 'index' }
>> end
>> end
>> end
>>
>> I am using "spec home_controller_spec.rb" by going under
>> spec/controllers/ directory.
>
> Don't do that :)
>
> Go to the project root directory and say "spec spec/controllers/
> home_controller_spec.rb"

Hi David,
I tried to run from the above command.
It gets executed but i am not able to see any results as in 2 examples 2
passed something like that as it comes normally.

David Chelimsky

unread,
Nov 12, 2009, 10:57:24 PM11/12/09
to rspec...@rubyforge.org
On Wed, Nov 11, 2009 at 11:19 PM, Amit Kulkarni <li...@ruby-forum.com> wrote:
David Chelimsky wrote:
> On Nov 11, 2009, at 5:58 AM, Amit Kulkarni <li...@ruby-forum.com> wrote:
>
>>
>>    it "should map { :controller => 'home' } RESTfully" do
>>      params_from( :get, '/home' ).should == { :controller => 'home',
>> :action => 'index' }
>>    end
>>  end
>> end
>>
>> I am using "spec home_controller_spec.rb" by going under
>> spec/controllers/ directory.
>
> Don't do that :)
>
> Go to the project root directory and say "spec spec/controllers/
> home_controller_spec.rb"

Hi David,
I tried to run from the above command.
It gets executed but i am not able to see any results as in 2 examples 2
passed something like that as it comes normally.

Then how do you know it gets executed?

Amit Kulkarni

unread,
Nov 13, 2009, 2:32:24 AM11/13/09
to rspec...@rubyforge.org
David Chelimsky wrote:
> On Wed, Nov 11, 2009 at 11:19 PM, Amit Kulkarni
> <li...@ruby-forum.com>wrote:
>
>> >>
>> It gets executed but i am not able to see any results as in 2 examples 2
>> passed something like that as it comes normally.
>>
>
> Then how do you know it gets executed?

I dont know,When i run the command no result is displayed.
Also error is not displayed.
But why is this happening?Also how can i run this to test my routes?

David Chelimsky

unread,
Nov 13, 2009, 2:56:53 AM11/13/09
to rspec...@rubyforge.org
On Thu, Nov 12, 2009 at 11:32 PM, Amit Kulkarni <li...@ruby-forum.com> wrote:
David Chelimsky wrote:
> On Wed, Nov 11, 2009 at 11:19 PM, Amit Kulkarni
> <li...@ruby-forum.com>wrote:
>
>> >>
>> It gets executed but i am not able to see any results as in 2 examples 2
>> passed something like that as it comes normally.
>>
>
> Then how do you know it gets executed?

I dont know,When i run the command no result is displayed.
Also error is not displayed.

That likely means nothing is actually happening. Please post your spec_helper.rb. Also, what versions of rspec, rspec-rails, and rails are you using?

Amit Kulkarni

unread,
Nov 13, 2009, 5:28:47 AM11/13/09
to rspec...@rubyforge.org
MY spec helper contains following code:
# This file is copied to ~/spec when you run 'ruby script/generate
rspec'
# from the project root directory.
ENV["RAILS_ENV"] ||= 'test'
require
File.expand_path(File.join(File.dirname(__FILE__),'..','config','environment'))
require 'spec'
require 'webrat'
require 'spec/autorun'
require 'spec/rails'

require File.join( Rails.root, 'spec', 'macros' )
# Uncomment the next line to use webrat's matchers
#require 'webrat/integrations/rspec-rails'

# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each
{|f| require f}

Spec::Runner.configure do |config|
config.include(ModelExampleGroupHelper, :type => :model)
config.include(ControllerExampleGroupHelper, :type => :controller)
# If you're not using ActiveRecord you should remove these
# lines, delete config/database.yml and disable :active_record
# in your config/boot.rb
config.use_transactional_fixtures = true
config.use_instantiated_fixtures = false
config.fixture_path = RAILS_ROOT + '/spec/fixtures/'

config.include Webrat::Matchers, :type => :views
# == Fixtures
#
# You can declare fixtures for each example_group like this:
# describe "...." do
# fixtures :table_a, :table_b
#
# Alternatively, if you prefer to declare them only once, you can
# do so right here. Just uncomment the next line and replace the
fixture
# names with your fixtures.
#
# config.global_fixtures = :table_a, :table_b
#
# If you declare global fixtures, be aware that they will be declared
# for all of your examples, even those that don't use them.
#
# You can also declare which fixtures to use (for example fixtures for
test/fixtures):
#
# config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
#
# == Mock Framework
#
# RSpec uses it's own mocking framework by default. If you prefer to
# use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
#
# == Notes
#
# For more information take a look at Spec::Runner::Configuration and
Spec::Runner
end

def login
controller.stub!( :logged_in? ).and_return( true )
controller.stub!( :login_required ).and_return( true )
@current_user = mock_model( User, :id => 1, :time_zone => 'Rome',
:idiom => 'it', :login => 'quentin', :email => 'f...@bar.com', :can_join?
=> true )
controller.stub!( :current_user ).and_return( @current_user )
end

def login_as( name )
controller.stub!( :logged_in? ).and_return( true )
controller.stub!( :login_required ).and_return( true )
@current_user = users(name)
controller.stub!( :current_user ).and_return( @current_user )
end

def valid_channel_attributes
{
:brand_name => 'RspecChannel'
}
end

def valid_user_attributes
{
:login => 'testtest', :password => '555555', :password_confirmation
=> '555555', :email => 'te...@test.com', :idiom => 'it', :privacy => '1',
}
end

def create_channel(args=nil)
args ||= valid_channel_attributes
Channel.create args
end

def create_user(args = nil)
args ||= valid_user_attributes
User.create args
end


#~ %w!User Contest Video Print Radio Banner Conceptl!.each do |model|
#~
model.constantize.any_instance.stubs(:save_attached_files).returns(true)
#~ end

rspec = 1.2.9
rspec-rails - 1.2.9
rails = 2.1.2

David Chelimsky

unread,
Nov 13, 2009, 9:39:51 AM11/13/09
to rspec...@rubyforge.org
There's your problem. The last version of rspec that supports rails 2.1.x is 1.1.12. See http://github.com/dchelimsky/rspec-rails/blob/master/History.rdoc.

Amit Kulkarni

unread,
Nov 16, 2009, 2:02:44 AM11/16/09
to rspec...@rubyforge.org
Oh that means if i am using rails version 2.1.2 or more then i need to
have rspec version 1.1.12.
In that case i need to remove the latest version and install rspec
1.1.12.
I will do it and update you asap.
Thanks a lot David.

Amit Kulkarni

unread,
Nov 17, 2009, 7:53:22 AM11/17/09
to rspec...@rubyforge.org
Amit Kulkarni wrote:
> Oh that means if i am using rails version 2.1.2 or more then i need to
> have rspec version 1.1.12.
> In that case i need to remove the latest version and install rspec
> 1.1.12.
> I will do it and update you asap.
> Thanks a lot David.

Hi David,
As per the doc i updated my gems.Version of my gems are :
Rails : 2.1.2
Rspec : 1.1.12
Rspec-Rails : 1.1.12

But when i run my files same error is being displayed.
I am working on Windows vista.Does this may impact running my spec?
Please suggest.

David Chelimsky

unread,
Nov 17, 2009, 10:11:14 AM11/17/09
to rspec...@rubyforge.org
On Tue, Nov 17, 2009 at 4:53 AM, Amit Kulkarni <li...@ruby-forum.com> wrote:
Amit Kulkarni wrote:
> Oh that means if i am using rails version 2.1.2 or more then i need to
> have rspec version 1.1.12.
> In that case i need to remove the latest version and install rspec
> 1.1.12.
> I will do it and update you asap.
> Thanks a lot David.

Hi David,
As per the doc i updated my gems.Version of my gems are :
Rails : 2.1.2
Rspec : 1.1.12
Rspec-Rails : 1.1.12

But when i run my files same error is being displayed.
I am working on Windows vista.Does this may impact running my spec?
Please suggest.

This back and forth is not productive. Please zip up the app and send it to me so I can see the whole thing in context.

Thanks,
David

Amit Kulkarni

unread,
Nov 17, 2009, 11:46:59 AM11/17/09
to rspec...@rubyforge.org
Ok Fine.That sounds to be a good idea.
Thanks

Amit Kulkarni

unread,
Nov 26, 2009, 2:21:27 AM11/26/09
to rspec...@rubyforge.org
Amit Kulkarni wrote:
> Ok Fine.That sounds to be a good idea.
> Thanks

Hi David,
Good news,My routing scenarios are working now.
I dont know how but it did.I was going to mail you the app but it is
working fine now.

David Chelimsky

unread,
Nov 26, 2009, 8:01:46 AM11/26/09
to rspec...@rubyforge.org
On Thu, Nov 26, 2009 at 1:21 AM, Amit Kulkarni <li...@ruby-forum.com> wrote:
Amit Kulkarni wrote:
> Ok Fine.That sounds to be a good idea.
> Thanks

Hi David,
Good news,My routing scenarios are working now.
I dont know how but it did.I was going to mail you the app but it is
working fine now.


Excellent! 

Amit Kulkarni

unread,
Dec 7, 2009, 5:32:27 AM12/7/09
to rspec...@rubyforge.org
Hi David,
I have some query regarding running spec command.
When i tried to run normal testcase by command "spec
test_controller_spec.rb"
then that particular test case runs but the routing testcase fails.

Now if i run through the command "spec
spec/controllers/test_controller_spec.rb" then all the spec runs
including the routing spec.

Can you tell me why is this happening?

David Chelimsky

unread,
Dec 7, 2009, 7:10:33 AM12/7/09
to rspec...@rubyforge.org
On Mon, Dec 7, 2009 at 5:32 AM, Amit Kulkarni <li...@ruby-forum.com> wrote:
Hi David,
I have some query regarding running spec command.
When i tried to run normal testcase by command "spec
test_controller_spec.rb"
then that particular test case runs but the routing testcase fails.

Now if i run through the command "spec
spec/controllers/test_controller_spec.rb" then all the spec runs
including the routing spec.

Can you tell me why is this happening?

rspec-rails creates different ExampleGroup subclasses for each type of spec (model, view, controller, helper). It knows which type to create based on the presence of "spec/controllers" (in this case) in the file's runtime path, so you need to run specs from the project root in order for rspec to make this determination.

HTH,
David

Amit Kulkarni

unread,
Dec 14, 2009, 2:18:17 AM12/14/09
to rspec...@rubyforge.org
Thanks David.
Also i am little bit confused regarding routes.

Consider a routing example

it "should map { :controller => 'channels', :action => 'new' } to
/channels/new" do
route_for(:controller => "channels", :action => "new").should ==
"/channels/new"
end

Now i can see all the routing methods by rake routes.In that i can see
this new action defined.

Now imagine if i write an action which is not defined in the rake routes
then instead of failing it is passing.For e.g.

it "should map { :controller => 'channels', :action => 'test' } to
/channels/test" do
route_for(:controller => "channels", :action => "test").should ==
"/channels/test"
end

Now here 'test' action is not defined in the routes.So it should fail
but it isn't.
May be i am missing something here.

Andrew Premdas

unread,
Dec 14, 2009, 5:01:14 AM12/14/09
to rspec...@rubyforge.org
2009/12/14 Amit Kulkarni <li...@ruby-forum.com>

Thanks David.
Also i am little bit confused regarding routes.

Consider a routing example

it "should map { :controller => 'channels', :action => 'new' } to
/channels/new" do
 route_for(:controller => "channels", :action => "new").should ==
"/channels/new"
end

Now i can see all the routing methods by rake routes.In that i can see
this new action defined.

Now imagine if i write an action which is not defined in the rake routes
then instead of failing it is passing.For e.g.

it "should map { :controller => 'channels', :action => 'test' } to
/channels/test" do
 route_for(:controller => "channels", :action => "test").should ==
"/channels/test"
end

Now here 'test' action is not defined in the routes.So it should fail
but it isn't.
May be i am missing something here.
--


Are you sure you have the default last two lines commented out in routes.rb. If you haven't they will match that action - I think.

All best

Andrew

Amit Kulkarni

unread,
Dec 24, 2009, 6:38:31 AM12/24/09
to rspec...@rubyforge.org
Yes,Thanks a lot Andrew.
The last two lines were not commented in routes.rb
because of that it was matching default.Now i commented out those line
and it was catching if undefined routes are given.
--
Reply all
Reply to author
Forward
0 new messages