Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined local variable or method `root_path'

Tags:

I have the following block of code in my rspec file located in the root of the /spec folder.

require 'spec_helper'  describe "home" do   subject { page }   before(:each) { visit root_path }   describe "page" do  it { should have_title('My Page')}  end  end 

When I run it I get

undefined local variable or method `root_path' 

Which doesn't make any sense. When I followed the rails tutorial a similar set up worked just fine. Can anyone help?

EDIT:

My routes includes

root "static#home" 

EDIT 2:

Reopening this topic. Moving my root declaration to the top did not fix it.

EDIT 3:

What worked was including url_helpers in my rspec config. I've never had to do this before. can anyone answer why this worked?

like image 514
user3389436 Avatar asked Mar 30 '14 08:03

user3389436


2 Answers

Named routes are not available in specs by default. Add the following code to spec_helper.rb:

RSpec.configure do |config|   config.include Rails.application.routes.url_helpers end 
like image 94
Gergo Erdosi Avatar answered Sep 19 '22 18:09

Gergo Erdosi


In your routes.rb, see if you have defined root. Something like this:

root :to => 'home#index' 
like image 44
Lenin Raj Rajasekaran Avatar answered Sep 23 '22 18:09

Lenin Raj Rajasekaran