Rails Tutorial static pages routes test red (5.28) - ruby-on-rails

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.

Related

Rails Test errors, while learning rails tutorial

I am currently learning Rails Tutorial. I have error messages after rails test, TDD. There are more messages, but first of all i want to debug these. Even though rails test is not working well, application itself is working without problem as of now.
I took a look this sample codes.
https://github.com/yasslab/sample_apps
Error messages
11) Error:
StaticPagesControllerTest#test_should_get_about:
NameError: undefined local variable or method `static_pages_about_url' for #<StaticPagesControllerTest:0x00000006f89228>
StaticPagesControllerTest#test_should_get_home:NameError: undefined local variable or method `static_pages_home_url' for #<StaticPagesControllerTest:0x00000006f964a0>
test/controllers/static_pages_controller_test.rb:7:in `block in <class:StaticPagesControllerTest>' 13) Error:
StaticPagesControllerTest#test_should_get_contact:NameError: undefined local variable or method `static_pages_contact_url' for #<StaticPagesControllerTest:0x0000000623d600>
test/controllers/static_pages_controller_test.rb:27:in `block in <class:StaticPagesControllerTest>' 14) Error:StaticPagesControllerTest#test_should_get_help:NameError: undefined local variable or method `static_pages_help_url' for #<StaticPagesControllerTest:0x00000006f9d6d8> test/controllers/static_pages_controller_test.rb:13:in `block in <class:StaticPagesControllerTest>'
static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def contact
end
end
static_pages_controller_test.rb
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get static_pages_home_url
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
test "should get help" do
get static_pages_help_url
assert_response :success
assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
end
test "should get about" do
get static_pages_about_url
assert_response :success
assert_select "title", "About | Ruby on Rails Tutorial Sample App"
end
test "should get contact" do
get static_pages_contact_url
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
end
end
routes.rb
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
end
I want to make these errors into assertions.
Could you kindly help me?

Ruby on Rails difference between get :home and static_pages_home_url

I am reading Ruby on Rails Tutorial (Ch 3.3.2) by Michael Hartl.
I am currently making a failing test(RED) However, I just noticed that his static_pages_controller_test.rb has
test "should get home" do
get :home
assert_response :success
end
and mine has although I followed everything he did.
test "should get home" do
get static_pages_home_url
assert_response :success
end
It seems like get :home is the same as get static_pages_home_url.
Is there any difference?
It does the same in this situation, but not in all situations.
since get :home is inside of your static_pages_controller_test.rb it looks in that controller automatically and finds the home method.
get static_pages_home_url is more dynamical. It looks for the home url inside of your static_pages_controller.
So you can call get_static_pages_home_url in tests on different locations but you can't with get :home.

Getting a rails test to pass

Following a rails tutorial but when off on a slight tangent and now cant get a test to pass.
Below is my routes file and my test file. The home test is failing and this line , to: "static_pages#home", as: "home" is the line that makes it appear. i.e remove this and it'll pass. I was wondering why someone could explain why this is failing and how to alter the test to make it pass but keeping this line in?
My routes file:
Rails.application.routes.draw do
resources :static_pages
get 'static_pages/help'
get 'static_pages/test'
get 'static_pages/home', to: "static_pages#home", as: "home"
root 'application#hello'
end
My test file:
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
get static_pages_home_url
assert_response :success
end
test "should get help" do
get static_pages_help_url
assert_response :success
end
test "should get test" do
get static_pages_test_url
assert_response :success
end
end
static_pages_home_url is the default helper method for that route with controller static_pages and action home but you set the name as home so your test should be
test "should get home" do
get home_url
assert_response :success
end

rails routing syntax errors

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'

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

Resources