Skip to content

Named Routes Unavailable Routing Specs #147

@logicaltext

Description

@logicaltext

I would like to test my named routes, but they don't seem to be available in specs defined in the spec/routing directory (though they are available in specs defined in the spec/requests directory). Here's a quick demo:

$ rails new test_app; cd test_app

Then append the following to the generated Gemfile:

group :development, :test do
  gem 'rspec', '>= 2.0.0.beta.19'
  gem 'rspec-rails', '>= 2.0.0.beta.19'
end

Then:

$ bundle install
$ rails g rspec:install
$ rails g scaffold User name:string

Finally, add the following route to config/routes.rb:

match "/fancy-path" => "users#index", :as => 'fancy'

Then, the following spec in spec/requests/users_spec.rb works:

require 'spec_helper'

describe "Users" do
  it "should recognize the fancy_path route" do
    fancy_path.should == '/fancy-path'
  end
end

But the following spec in spec/routing/users_routing_spec.rb does not work:

require 'spec_helper'

describe UsersController do
  describe "routing" do
    it "recognizes fancy_path" do
      { :get => fancy_path }.should route_to(:controller => "users", :action => "index")
    end
  end
end

I get the following error:

undefined local variable or method `fancy_path' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_1:0x102c17c00>

Is RSpec not loading the named routes for routing specs, or am I missing something? Any help would be much appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions