Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec and named routes

I'm quite new to rails, and trying to follow the railstutorial. Everything goes fine, except for my tests which can't get past the named routes (5.3.3)

My routes.rb :

 SampleApp::Application.routes.draw do

 resources :users
 match '/signup',  to: 'users#new'

 match '/help',    to: 'static_pages#help'
 match '/about',   to: 'static_pages#about'
 match '/contact', to: 'pages#contact'

 root to: 'static_pages#home'

 #Commented stuff

My firsts tests (spec/controllers/static_pages_controller_spec.rb) :

describe "Static pages" do

subject { page }

shared_examples_for "all static pages" do
  it { should have_selector('h1',    text: heading) }
  it { should have_selector('title', text: full_title(page_title)) }
end

describe "Home page" do
  before { visit root_path }
  let(:heading)    { 'Sample App' }
  let(:page_title) { 'Home' }

  it_should_behave_like "all static pages"
end

#Other tests

The spec_helper.rb looks like (without all the commented stuff)

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true
    config.infer_base_class_for_anonymous_controllers = false
end

The errors I get from rspec are all like this one :

 Static pages Home page it should behave like all static pages 
 Failure/Error: before { visit root_path }
 NameError:
   undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1::Nested_1:0x00000004a12210>
 Shared Example Group: "all static pages" called from ./spec/controllers/static_pages_controller_spec.rb:17
 # ./spec/controllers/static_pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

I already tried using

 include Rails.application.routes.url_helpers

in the spec_helper, but it changed my errors to

 Static pages Home page it should behave like all static pages 
 Failure/Error: Unable to find matching line from backtrace
 SystemStackError:
   stack level too deep
 # /usr/lib/ruby/1.9.1/forwardable.rb:185

I also tried different way of renaming my routes, but none of them worked. I'm back to the tutorial version.

If it can be of any help in finding what exactly is a problem, I'm on Ubuntu 11.10, with rails 3.2.1 and ruby 1.9.2p290. Hope you can help, I spend quite a while googling for a solution and didn't find any ^^'

like image 696
FabPelletier Avatar asked Feb 28 '12 03:02

FabPelletier


4 Answers

Named routes should work if you put the following in rspec_helper.rb:

RSpec.configure do |config|
  config.include Rails.application.routes.url_helpers
  ...
end

Is that how you set it up?

like image 188
Tom L Avatar answered Nov 15 '22 11:11

Tom L


I don't think you have access to named routes inside of your rspec controller specs. You could however just do visit('/'), which is the equivalent of root_path.

like image 37
TheDelChop Avatar answered Nov 15 '22 10:11

TheDelChop


Google brought me here, even my error message doesn't fit 100%.

In my case Capybara command visitis unknown...

Error:

NoMethodError:
       undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xa49a73c>

Since Capybara 2.0 one has to use folder spec/features capybara commands don't work in folder spec/requests anymore.

That helped me: http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html

Hope you find this usefull.

like image 3
BBQ Chef Avatar answered Nov 15 '22 10:11

BBQ Chef


I had the same problem, with the same Tutorial. It turns out that I needed to just restart the Spork service, and all worked fine.

The solution posted by Tom L worked for me, but when I removed that line and restarted Spork, that also fixed the problem.

Hope that helps out any other people who are wary about deviating from the tutorial's code!

like image 2
csalvato Avatar answered Nov 15 '22 11:11

csalvato