Stack overflow at LayoutLinks specs from railstutorial.org - ruby-on-rails

I'm trying to follow THE magnificient Rails3 tutorial at http://railstutorial.org but ran into a problem in chapter 5.
How ironic, but i'm getting stack overflow running these LayoutLinks (http://railstutorial.org/chapters/filling-in-the-layout#sec:integration_tests) specs.
This is the output:
C:\sample_app>rspec spec\requests\layout_links_spec.rb
FFFFFF
Failures:
1) LayoutLinks should have a Home page at '/'
Failure/Error: response.should have_selector('title', :content => "Home")
stack level too deep
# ./spec/requests/layout_links_spec.rb:7
2) LayoutLinks should have a Contact page at '/contact'
Failure/Error: response.should have_selector('title', :content => "Contact")
stack level too deep
# ./spec/requests/layout_links_spec.rb:12
3) LayoutLinks should have an About page at '/about'
Failure/Error: response.should have_selector('title', :content => "About")
stack level too deep
# ./spec/requests/layout_links_spec.rb:17
4) LayoutLinks should have a Help page at '/help'
Failure/Error: response.should have_selector('title', :content => "Help")
stack level too deep
# ./spec/requests/layout_links_spec.rb:22
5) LayoutLinks should have a Help page at '/help'
Failure/Error: response.should have_selector('title', :content => "Help")
stack level too deep
# ./spec/requests/layout_links_spec.rb:27
6) LayoutLinks should have a signup page at '/signup'
Failure/Error: response.should have_selector('title', :content => "Sign up")
stack level too deep
# ./spec/requests/layout_links_spec.rb:32
Finished in 206.38 seconds
6 examples, 6 failures
The code and specs ought to be the same as on that tutorial (i copy-pasted).
Any ideas what might be happening here?
I tried to run specs with -b to get more stacktrace but it didn't work for some reason.
I'm using Ruby 1.8.7, Rails 3.0.1 and RSpec 2.1.0 on Windows 7.

Seems like you are running into this Webrat issue: https://github.com/rspec/rspec-rails/issues#issue/140.
Try downgrading to Webrat 0.7.1 in your gemfile.

Related

Rails Tutorial static pages routes test red (5.28)

Doing Michael Hartl's Rails Tutorial, but stumped on Listing 5.28 (getting RED test instead of GREEN), changing the test to match the new routes.
ERRORS for all pages (/, /about, /contact, /help):
ActionController::UrlGenerationError: No route matches {:action=>"/*", :controller=>"static_pages"}
routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
end
tests/controllers/static_pages_controller_test.rb
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get root_path
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
test "should get help" do
get help_path
assert_response :success
assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
end
test "should get about" do
get about_path
assert_response :success
assert_select "title", "About | Ruby on Rails Tutorial Sample App"
end
test "should get contact" do
get contact_path
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
end
end
static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def contact
end
end
Let me know if you need to see any other code! Have tried adding as: '*' after each get route, but to no avail.
Not sure if it is a ruby/rails version issue, but I am using Rails 4.2.2 and Ruby 2.3.0 on an IDE, but "rails test" (as Hartl instructs to use) won't work (kicks back "test Command not found"). Not sure if that's a hint to a bigger problem or unrelated. Thanks in advance!
EDIT: Links using these paths (like below) are rendering correctly, it is just failing the tests.
<%= link_to "Home", root_path %>
<%= link_to "Help", help_path %>
Your test/controllers/static_pages_controller_test.rb have problem. I would suggest to replace it with below contents.
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
get root_path
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
test "should get help" do
get help_path
assert_response :success
assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
end
test "should get about" do
get about_path
assert_response :success
assert_select "title", "About | Ruby on Rails Tutorial Sample App"
end
test "should get contact" do
get contact_path
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
end
end
Change the above file as I have suggested and run $ rails test you can see green test. I also suggest you to upgrade your app to rails 5.0.0 as Michael Hartl's have been upgraded his rails tutorials to rails 5.0.0 in future you have more things to learn and if you upgrade it, your journey of learning would be more error free and pleasant.

RoR and Rspec setting up a test using a while loop

Ok so I want to create a test for checking that all pages have a certain title. However I thought it would be nice if I could include the page titles in an array so that I wouldn't have to duplicate the block for each page. And it would allow me to test additional pages by just modifying the pages array.
The issue I am having is that the page variable is not being interpolated in the test. So is this a syntax error or does Rspec not allow interpolation within the it should do... block?
describe "LayoutLinks" do
page = ["home", "contact", "about", "help"]
i = page.count
x = 0
while x < i
it "should have a #{page[x]} page" do
get "#{page[x]}"
response.should have_selector("title", :content => "#{page[x]}")
end
x += 1
end
end
Test shows the following failures:
1) PagesController LayoutLinks should have a help page
Failure/Error: get "#{page[x]}"
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>""}
# ./spec/controllers/layout_links_spec.rb:14:in `block (3 levels) in <top (required)>'
2) PagesController LayoutLinks should have a contact page
Failure/Error: get "#{page[x]}"
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>""}
# ./spec/controllers/layout_links_spec.rb:14:in `block (3 levels) in <top (required)>'
3) PagesController LayoutLinks should have a about page
Failure/Error: get "#{page[x]}"
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>""}
# ./spec/controllers/layout_links_spec.rb:14:in `block (3 levels) in <top (required)>'
4) PagesController LayoutLinks should have a home page
Failure/Error: get "#{page[x]}"
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>""}
Failure error here is obvious. It shouldn't say get "#{page[x]}" but rather it should be get home and get about, etc...
How do I remedy? Thanks for the help. Much appreciated :)
Try the following:
describe "LayoutLinks" do
%w(home contact about help).each do |page|
it "should have a #{page} page" do
get page
response.should have_selector("title", content: page)
end
end
end
%w creates an array of strings (spaces become commas, so you get ["home", "contact", etc])

Hartl's Tutorial Section 5.3.2: Rails Routes

I've been following along in the tutorial exactly as it has been written. So far everything has gone without a hitch, until this section.
I was supposed to change the "GET" statements in the config/routes.rb file to the following:
SampleApp::Application.routes.draw do
root to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
This was supposed to make the tests pass. They do not pass. They continue to fail with the following error as one of the 9 similar errors:
Failure/Error: visit about_path
NameError:
undefined local variable or method 'about_path' ....
I have no idea how to get this to pass so that I can move on. What did I miss? What did Hartl miss? Other people who have asked this question never got an answer that made any sense or even worked when tried.
Before anyone asks:
All versions of Rails, Ruby and other installed components are the exact same versions used in the tutorial as it is written today 2012-10-05. Everything matches the tutorial perfectly.
UPDATE: Here is the current static_pages_spec.rb file
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the h1 'Sample App'" do
visit root_path
page.should have_selector('h1', text: 'Sample App')
end
it "should have the base title" do
visit root_path
page.should have_selector('title',
text: "Ruby on Rails Tutorial Sample App")
end
it "should not have a custom page title" do
visit root_path
page.should_not have_selector('title', text: '| Home')
end
end
describe "Help page" do
it "should have the h1 'Help'" do
visit help_path
page.should have_selector('h1', text: 'Help')
end
it "should have the title 'Help'" do
visit help_path
page.should have_selector('title',
text: "Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About page" do
it "should have the h1 'About'" do
visit about_path
page.should have_selector('h1', text: 'About Us')
end
it "should have the title 'About Us'" do
visit about_path
page.should have_selector('title',
text: "Ruby on Rails Tutorial Sample App | About Us")
end
end
describe "Contact page" do
it "should have the h1 'Contact'" do
visit contact_path
page.should have_selector('h1', text: 'Contact')
end
it "should have the title 'Contact'" do
visit contact_path
page.should have_selector('title',
text: "Ruby on Rails Tutorial Sample App | Contact")
end
end
end
Rake Routes results:
root / static_pages#home
help /help(.:format) static_pages#help
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
Try changing in your routes.rb your line to:
match '/about', to: 'static_pages#about', as: 'about'
and also you should reload Spork in order to changes apply.
You can also add:
load "#{Rails.root}/config/routes.rb"
into your Spork.each_run block in order to reload routes each time you run Spork.
Reloading spork worked for me.
Hi try to add this on your spec.
describe "Static pages" do
include Rails.application.routes.url_helpers
.......
I think you should type in the browser
http://localhost:3000/
instead of http://localhost:3000/static_pages/home to get the home page for example.
I think the problem is not the code but rather how to access the page. In the sections prior to 5.3.2 in the tutorial, you needed to visit localhost:3000/static_pages/home to access the homepage. After you follow instructions in 5.3.2, you only need to type http://localhost:3000/.
I just encountered this error, and had some help fixing this. I had mistakenly added the signin_path and signup_path on the layouts/_header.html.erb and static_pages/home.html.erb, respectively. This had EVERY page confused as to what the signin_path was.
The other issue I had made an error on was the root path. If you run 'rm public/index.html' from your sample app directory, 'root 'static_pages#home' in routes.rb, should work. The problem is that things in the public directory override any other part of your app as you customize it. Hope this helps you like it helped me :)
Named routes are not available in specs by default. Add the following code to the spec_helper.rb file:
RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
end

RSpec Error while making RoR tutorial

I got an error while testing code from RoR tutorial, pls help with these issue, listing is below. May be wrong version of spec in Gemfile.
Failures:
1) PagesController GET 'home' should have the right title
Failure/Error: response.should have_selector("title", :content => "Home")
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0x007fb3d4d2d108>
# ./spec/controllers/pages_controller_spec.rb:14:in `block (3 levels) in <top (required)>'
2) PagesController GET 'contact' should have the right title
Failure/Error: response.should have_selector("title", :content => "Contact")
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0x007fb3d280b370>
# ./spec/controllers/pages_controller_spec.rb:26:in `block (3 levels) in <top (required)>'
3) PagesController GET 'about' should have the right title
Failure/Error: response.should have_selector("title", :content => "About")
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0x007fb3d2b96e90>
# ./spec/controllers/pages_controller_spec.rb:38:in `block (3 levels) in <top (required)>'
4) PagesController GET 'help' should have the right title
Failure/Error: response.should have_selector("title", :content => "Help")
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0x007fb3d28a19d8>
# ./spec/controllers/pages_controller_spec.rb:50:in `block (3 levels) in <top (required)>'
Finished in 0.1005 seconds
8 examples, 4 failures
Failed examples:
rspec ./spec/controllers/pages_controller_spec.rb:12 # PagesController GET 'home' should have the right title
rspec ./spec/controllers/pages_controller_spec.rb:24 # PagesController GET 'contact' should have the right title
rspec ./spec/controllers/pages_controller_spec.rb:36 # PagesController GET 'about' should have the right title
rspec ./spec/controllers/pages_controller_spec.rb:48 # PagesController GET 'help' should have the right title
Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.3'
gem 'sqlite3'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
group :test, :development do
gem "rspec-rails", "~> 2.10.1"
end
pages_controller_spec.rb listing:
require 'spec_helper'
describe PagesController do
render_views
describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
it "should have the right title" do
get 'home'
response.should have_selector("title", :content => "Home")
end
end
describe "GET 'contact'" do
it "should be successful" do
get 'contact'
response.should be_success
end
it "should have the right title" do
get 'contact'
response.should have_selector("title", :content => "Contact")
end
end
describe "GET 'about'" do
it "should be successful" do
get 'about'
response.should be_success
end
it "should have the right title" do
get 'about'
response.should have_selector("title", :content => "About")
end
end
describe "GET 'help'" do
it "should be successful" do
get 'help'
response.should be_success
end
it "should have the right title" do
get 'help'
response.should have_selector("title", :content => "Help")
end
end
end
I do not understang what is wrong.
If you don't want to use webrat, you can use
response.body.should include 'Contact</h1>'
Rather than
response.should have_selector("title", :content => "Contact")
Which is a bit less clean, I agree.
Re-edit your GEMFILE and type
gem 'webrat'
and then
type
bundle install

Integration Testing With Ruby on Rails Tutorial Issue

I'm trying to follow the ruby on rails tutorial and impliment the Integration Testing.
I first run the command: bundle exec rspec spec/
And it tells me all but one of my sixteen tests passes. Here is the part where I think the issue is:
require 'spec_helper'
describe "LayoutLinks" do
it "should have the right links on the layout" do
visit root_path
click_link "Help"
response.should have_selector('title', :content => "Help")
click_link "Contact"
response.should have_selector('title', :content => "Contact")
click_link "Home"
response.should have_selector('title', :content => "Home")
click_link "Sign up now!"
response.should have_selector('title', :content => "Sign Up")
click_link "About"
response.should have_selector('title', :content => "About")
end
end
As a result I get the following:
Failure/Error: response.should have_selector('title', :content => "Help")
expected following output to contain a <title>Help</title> tag:
#home.html.erb page's source shown
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Ruby on Rails Tutorial Sample App | Home</title>
#The line above is why the test fails.
#It is loading home.html.erb instead of help.html.erb
.
.
.
# ./spec/requests/layout_links_spec.rb:34:in `block (2 levels) in <top (required)>'
I can move around the order of the tests and it is always the top test that fails. This makes me believe there is something wrong here and not with the actual rails code. I can also go to the demo website and the links work and they go to the correct pages. I have looked at the other issues other people have had with this and I can't seem to find anyone who is having the same issues. How can I go about trouble shooting this?
Update:
noahc:sample_app noahc$ rake routes
users_new GET /users/new(.:format) {:controller=>"users", :action=>"new"}
signup /signup(.:format) {:controller=>"users", :action=>"new"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
/help(.:format) {:controller=>"pages", :action=>"help"}
root / {:controller=>"pages", :action=>"home"}
pages_home GET /pages/home(.:format) {:controller=>"pages", :action=>"home"}
pages_contact GET /pages/contact(.:format) {:controller=>"pages", :action=>"contact"}
pages_about GET /pages/about(.:format) {:controller=>"pages", :action=>"about"}
pages_help GET /pages/help(.:format) {:controller=>"pages", :action=>"help"}
Noah, I would recommend splitting that test up into multiple tests, one for each title that you would like to test. That will make it a little easier for you to pinpoint exactly what's wrong. Something like this:
it "should have a link to 'Help'" do
visit root_path
response.should have_selector('a', :href => help_path, :content => "Help")
click_link "Help"
response.should have_selector('title', :content => 'Help')
end
it "should have a link to 'Contact'" do
visit root_path
response.should have_selector('a', :href => contact_path, :content => "Contact")
click_link "Help"
response.should have_selector('title', :content => 'Contact')
end
etc... This will make it easier to pinpoint exactly what is going on, and where you're experiencing a problem. Also, what did it say was being displayed in the page's source? You cut off the error message before it explained what really happened in your paste snippet... Look inside what the code that is actually being returned says to see what the title is and how/why it is differing from what rspec is expecting.
I don't use rspec, but could be because "Home" != "Ruby on Rails Tutorial Sample App | Home"
The contents do not match at all, so the test fails.
Also, check your help link in your source to be sure it is in fact going where you think it is...

Resources