Undefined method `route_for

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

On Tue, Nov 10, 2009 at 4:44 AM, Amit K. [email protected]
wrote:

describe “route generation” do
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

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 :slight_smile:

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.

On Wed, Nov 11, 2009 at 1:42 AM, Amit K. [email protected]
wrote:

/home" do
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 :slight_smile:

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

On Nov 11, 2009, at 5:58 AM, Amit K. [email protected] 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 :slight_smile:

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

David C. wrote:

On Nov 11, 2009, at 5:58 AM, Amit K. [email protected] 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 :slight_smile:

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.

On Wed, Nov 11, 2009 at 11:19 PM, Amit K.
[email protected]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?

On Thu, Nov 12, 2009 at 11:32 PM, Amit K.
[email protected]wrote:

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?

David C. wrote:

On Wed, Nov 11, 2009 at 11:19 PM, Amit K.
[email protected]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?

On Fri, Nov 13, 2009 at 2:28 AM, Amit K. [email protected]
wrote:

require ‘spec/autorun’
{|f| require f}

names with your fixtures.

controller.stub!( :current_user ).and_return( @current_user )
{

#~ %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

There’s your problem. The last version of rspec that supports rails
2.1.x is
1.1.12. See
rspec-rails/History.rdoc at master · dchelimsky/rspec-rails · GitHub.

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 => ‘[email protected]’, :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 => ‘[email protected]’, :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

Amit K. 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.

On Tue, Nov 17, 2009 at 4:53 AM, Amit K. [email protected]
wrote:

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

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.

Ok Fine.That sounds to be a good idea.
Thanks

Amit K. 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.

On Thu, Nov 26, 2009 at 1:21 AM, Amit K. [email protected]
wrote:

Amit K. 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!

On Mon, Dec 7, 2009 at 5:32 AM, Amit K. [email protected]
wrote:

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

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?