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...
Related
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.
I am working on Michael Hartl's tutorial at railstutorial.org. I am having Difficulty In chapter 5 with getting the routing to work.
If I start with a routes file
routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
get 'static_pages/contact'
for each of these there is a test like
static_pages_controller_test.rb
test "should get home" do
get :home
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
this syntax works and all the tests pass but later he wants to change the syntax using the *_path convention.
so now the tests look like
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get root_path
.
.
end
test "should get help" do
get help_path
.
.
end
and I updated the routes to
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
but now all the tests fail with the messages
ERROR["test_should_get_home", StaticPagesControllerTest, 2016-06-30 05:02:41 -0700]
test_should_get_home#StaticPagesControllerTest (1467288161.43s)
ActionController::UrlGenerationError: ActionController::UrlGenerationError:
No route matches {:action=>"/", :controller=>"static_pages"}
ERROR["test_should_get_help", StaticPagesControllerTest, 2016-06-30 05:02:41 -0700]
test_should_get_help#StaticPagesControllerTest (1467288161.43s)
ActionController::UrlGenerationError: ActionController::UrlGenerationError:
No route matches {:action=>"/help", :controller=>"static_pages"}
my controller looks something like this
class StaticPagesController < ApplicationController
def home
end
def help
end
.
.
end
if I run rake routes I get
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
what am I doing wrong?
You need to rewrite these routes so that it can create dynamic route helpers for you as per your test. Write it like,
get 'static_pages/help' , as: :help
get 'static_pages/about' , as: :about
get 'static_pages/contact' , as: :contact
Read 3.6 Naming Routes.
As per your current route, those *_path will be like static_pages_about, static_pages_help etc. I am not sure how did you get the rake routes output like you have shown without using as option.
Yes Author has updated rails tutorials with 5.0.0 last week. It is suggested you update it too which will make further journey more pleasant and error free in addition to that, you will get more new things to learn in 5.0.0
update your tests/controllers/static_pages_controller_test.rb
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
I am sure once you update your tests/controllers/static_pages_controller_test.rb you will see green test $ rails test
I'm not so sure, but what happens if you do this:
root 'static_pages#home'
get 'help', to: 'static_pages#help'
get 'about', to: 'static_pages#about'
get 'contact', to: 'static_pages#contact'
I have spend a few hours going over the book and the code from the github page, and I cannot seem to find my answer. I am in chapter 7.1.3 and trying to get the following tests to pass. When I view the pages everything is working properly as far as I can tell.
user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_selector('h1', text: 'Sign Up') } (10)
it { should have_selector('title', text: full_title('Sign Up')) } (11)
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_selector('h1', text: 'user.name') } (18)
it { should have_selector('title', text: 'user.name') } (19)
end
end
I have been through all of the files that I can think of checking what could go wrong.
app/views/users/new.html.erb
<% provide(:title, 'Sign Up') %>
<h1>Sign Up</h1>
<p>Find me in app/views/users/new.html.erb</p>
**app/views/users/show.html.erb
<% provide(:title, #user.name) %>
<h1><%= #user.name %></h1>
app/controller/users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def new
end
end
config/routes.rb
SampleApp::Application.routes.draw do
resources :users
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/signup', :to => 'users#new'
root :to => 'pages#home'
I think I have attached all of the relevant code, please let me know if adding anything else will help. The following are the actual errors that I am getting. If you can please point out where I have thrown a wrench in the works I would be very appreciative.
rspec ./spec/requests/user_pages_spec.rb:10 # User pages signup page
rspec ./spec/requests/user_pages_spec.rb:11 # User pages signup page
rspec ./spec/requests/user_pages_spec.rb:18 # User pages profile page
rspec ./spec/requests/user_pages_spec.rb:19 # User pages profile page
Thanks for your help and time!
Remove the quotation marks in line 18 and 19. That should pass your tests.
Like this:
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
end
Also look for your routes.rb. In the rails tutorial it is well
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
unless you have create the static pages views with the name pages. Then forget my routes.rb
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
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.