I'm ploughing through Michael Hartl's Ruby on Rails Tutorial (https://www.railstutorial.org/book/filling_in_the_layout) right now and found an issue I can't google fix.
Upon creating this file: test/helpers/application_helper_test.rb, and running:
'rails test'
I'm getting this failure:
AIL["test_full_title_helper", ApplicationHelperTest, 0.7209667120041559]
test_full_title_helper#ApplicationHelperTest (0.72s)
--- expected
+++ actual
## -1 +1 ##
-"Help | Ruby on Rails Tutorial Sample App"
+"Ruby on Rails Tutorial Sample App"
test/helpers/application_helper_test.rb:6:in `block in <class:ApplicationHelperTest>'
Having browsed similar questions, albeit questions over a year old (and using older Rails code), I'm none the wiser as to how to rectify this failure.
I am clearly a Ruby noobie, so please be gentle :) any help would be greatly appreciated
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
and the offending file:
require 'test_helper'
class ApplicationHelperTest < ActionView::TestCase
test "full title helper" do
assert_equal full_title, "Ruby on Rails Tutorial Sample App"
assert_equal full_title("Help"), "Help | Ruby on Rails Tutorial Sample App"
end
end
and the site_layout_test.rb
require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
test "layout links" do
get root_path
assert_template 'static_pages/home'
assert_select "a[href=?]", root_path, count: 2
assert_select "a[href=?]", help_path
assert_select "a[href=?]", about_path
assert_select "a[href=?]", contact_path
get contact_path
assert_select "title", full_title("Contact")
end
end
and the code for the application_helper:
module ApplicationHelper
# Returns the full title on a per-page basis.
def full_title(page_title = '')
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
page_title + " | " + base_title
end
end
end
help.html.erb static_page:
<% provide(:title, "Help") %>
<h1>Help</h1>
<p>
Get help on the Ruby on Rails Tutorial at the
Rails Tutorial help section.
To get help on this sample app, see the
<a href="http://www.railstutorial.org/book"><em>Ruby on Rails Tutorial</em>
book</a>.
</p>
I'm working on the same book, and am at the same chapter as you! I see the following:
--- expected
+++ actual
## -1 +1 ##
-"Help | Ruby on Rails Tutorial Sample App"
+"Ruby on Rails Tutorial Sample App"
Rearranged:
--- expected
-"Help | Ruby on Rails Tutorial Sample App"
+++ actual
+"Ruby on Rails Tutorial Sample App"
The test expected "Help | Ruby on Rails Tutorial Sample App", but actually got "Ruby on Rails Tutorial Sample App".
If you look at the help page in a browser, what shows up in the title bar? The help.html.erb looks right to me.
I also have the following for my app/helpers/application_helper.rb, is a little different from yours:
module ApplicationHelper
# Returns the full title on a per-page basis
def full_title(page_title = '')
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
page_title + " | " + base_title
end
end
end
Here is my full_title method.
module ApplicationHelper
def full_title(page_title = "")
base_title = "Ruby on Rails Tutorial Sample App"
line = page_title.empty? ? "" : " | "
return "#{page_title}#{line}#{base_title}"
end
end
Related
I keep running this test and it continually fails, yet I cannot find an error. The failure message is:
[Failure:
StaticPagesControllerTest#test_should_get_contact [/Users/iThompkins/Documents/SiteDev/environment/sample_app/test/controlle
rs/static_pages_controller_test.rb:26]:
<Contact | Ruby on Rails Tutorial Sample App> expected but was
<Ruby on Rails Tutorial Sample App>..
Expected 0 to be >= 1.]
My test looks like:
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
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
My controller
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def contact
end
end
And my View
<% provide(:title, 'Contact') %>
Contact
Contact the Ruby on Rails Tutorial about the sample app at the
contact page.
Have you got <%= yield(:title) %> in the title section of your view?
<title><%= yield(:title) %> | Ruby on Rails Tutorial Sample App</title>
From the Hartl book, you need to yield the title value to get it to display.
I am following a Rails tutorial..
Here is my testing file
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
def setup
#base_title = "Ruby on Rails Tutorial Sample App"
end
test "should get home" do
get static_pages_home_url
assert_response :success
assert_select "title", "Home | #{#base_title}"
end
test "should get help" do
get static_pages_help_url
assert_response :success
assert_select "title", "Help | #{#base_title}"
end
test "should get about" do
get static_pages_about_url
assert_response :success
assert_select "title", "About | #{#base_title}"
end
end
This is the error message i get:
Failure:
StaticPagesControllerTest#test_should_get_home
[c:/Sites/workspace/sample_app/te
st/controllers/static_pages_controller_test.rb:12]:
<Home | Ruby on Rails Tutorial Sample App> expected but was
<Home>..
Expected 0 to be >= 1.
Do take a not that the error sometimes shows About/Help in place of Home.
BTW when i comment out lines 5 to 7 and replace #{#base_title} in each of the tests the rails test result is success then.
Really conflicted with what is going on here.
I'm completing the exercise to add a Contact page, but the testing fails on the page title.
Here is my testing file:
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
def setup
#base_title = "Ruby on Rails Tutorial Sample App"
end
test "should get root" do
get root_url
assert_response :success
end
test "should get home" do
get static_pages_home_url
assert_response :success
assert_select "title", "Home | #{#base_title}"
end
test "should get help" do
get static_pages_help_url
assert_response :success
assert_select "title", "Help | #{#base_title}"
end
test "should get about" do
get static_pages_about_url
assert_response :success
assert_select "title", "About | #{#base_title}"
end
test "should get contact" do
get static_pages_about_url
assert_response :success
assert_select "title", "Contact | #{#base_title}"
end
end
Here is the contact.html.erb file:
<% provide(:title, "Contact") %>
<h1>Contact</h1>
<p>
Contact the Ruby on Rails Tutorial about the sample app at the
contact page.
</p>
I've also completed the following:
Added the appropriate route
Added the appropriate action
However I get this error message:
test_should_get_contact#StaticPagesControllerTest (0.45s)
<Contact | Ruby on Rails Tutorial Sample App> expected but was
<About | Ruby on Rails Tutorial Sample App>..
Expected 0 to be >= 1.
test/controllers/static_pages_controller_test.rb:35:in `block in <class:StaticPagesControllerTest>'
Please also note that
The page displays correctly, with the expected page title (Contact not About)
I tested again using a completely new page, but had the same result with 'About' being returned in page title
Really not sure why it's returning this as I've followed Tutorial closely. I want to progress in Tutorial, but if I cannot resolve this basic testing issue, I'm not sure I'll get very far!
Please check your code on the second line of this code block.
test "should get contact" do
# get static_pages_about_url # This is wrong correct it to as below
get static_pages_contact_url
assert_response :success
assert_select "title", "Contact | #{#base_title}"
end
You have given a test case to check the contact page title on the about url which obviously will fail the test.
You should be testing for contact page title on the contact url like above.
Make the change and you should get going!
Also a word of motivation, just keep going even if things don't make sense right now cause later they will. Cheers :)
I think you might try to replace the line get static_pages_about_url (under test "should get contact" do) with:
get static_pages_contact_url
What happens is that your test is calling the wrong url (about, instead of contact), causing the error when checking the <title>.
I keep getting a few errors that I have seen from other people on here. Here is the error message:
1) Failure:
StaticPagesControllerTest#test_should_get_about [C:/Sites/sample_app/test/controllers/static_pages_controller_test.rb:19]:
<About | Ruby on Rails Tutorial Sample App> expected but was
<About | Ruby on Rails Tutorial Sample App.>.
Expected 0 to be >= 1.
2) Failure:
StaticPagesControllerTest#test_should_get_contact [C:/Sites/sample_app/test/controllers/static_pages_controller_test.rb:25]:
<Contact | Ruby on Rails Tutorial Sample App> expected but was
<Contact | Ruby on Rails Tutorial Sample App.>.
Expected 0 to be >= 1.
3) Failure:
StaticPagesControllerTest#test_should_get_help [C:/Sites/sample_app/test/controllers/static_pages_controller_test.rb:13]:
<Help | Ruby on Rails Tutorial Sample App> expected but was
<Help | Ruby on Rails Tutorial Sample App.>.
Expected 0 to be >= 1.
4) Failure:
StaticPagesControllerTest#test_should_get_home [C:/Sites/sample_app/test/controllers/static_pages_controller_test.rb:7]:
<Home | Ruby on Rails Tutorial Sample App> expected but was
<Ruby on Rails Tutorial Sample App.>.
Expected 0 to be >= 1.
Here is also code for the test file:
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get :home
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
test "should get help" do
get :help
assert_response :success
assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
end
test "should get about" do
get :about
assert_response :success
assert_select "title", "About | Ruby on Rails Tutorial Sample App"
end
test "should get contact" do
get :contact
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
end
end
How do I fix this problem on my application? Do I have to write another part of code for the program?
You should try adding a . (period/full stop) after Sample App in your tests, or else removing the . from your view.
Like this:
test "should get home" do
get :home
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App."
end
the message Expected 0 to be >= 1 simply means the test was counting how many times it could find the content (without the .) on the page and it counted 0 but it expected the count to be at least 1 since that is what you assert in you test.
I am trying to complete exercise 4 in Chapter 5 of the Hartl tutorial and I am getting these two errors when I run $bundle exec rake test:
Started
ERROR["test_layout_links", SiteLayoutTest, 2015-03-31 09:33:35 +0000]
test_layout_links#SiteLayoutTest (1427794415.88s)
NoMethodError: NoMethodError: undefined method `full_title' for # <SiteLayoutTest:0x00000006cee338>
test/integration/site_layout_test.rb:13:in `block in <class:SiteLayoutTest>'
test/integration/site_layout_test.rb:13:in `block in <class:SiteLayoutTest>'
ERROR["test_full_title_helper", ApplicationHelperTest, 2015-03-31 09:33:35 +0000]
test_full_title_helper#ApplicationHelperTest (1427794415.99s)
ArgumentError: ArgumentError: wrong number of arguments (0 for 1)
app/helpers/application_helper.rb:2:in `full_title'
test/helpers/application_helper_test.rb:5:in `block in <class:ApplicationHelperTest>'
app/helpers/application_helper.rb:2:in `full_title'
test/helpers/application_helper_test.rb:5:in `block in <class:ApplicationHelperTest>'
7/7: [====================================================================================================================] 100% Time: 00:00:00, Time: 00:00:00
Finished in 0.57888s
7 tests, 14 assertions, 0 failures, 2 errors, 0 skips
What do these errors mean, and how do I go about fixing the errors?
This is my test/integration/site_layout_test.rb code:
require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
test "layout links" do
get root_path
assert_template 'static_pages/home'
assert_select "a[href=?]", root_path, count: 2
assert_select "a[href=?]", help_path
assert_select "a[href=?]", about_path
assert_select "a[href=?]", contact_path
get signup_path
assert_select "title", full_title("Sign up")
end
end
And this is my app/helpers/application_helper.rb code:
module ApplicationHelper
def full_title(page_title)
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
" #{page_title} | #{base_title}"
end
end
end
This is my test/helpers/application_helper_test.rb file:
require 'test_helper'
class ApplicationHelperTest < ActionView::TestCase
test "full title helper" do
assert_equal full_title, "Ruby On Rails App"
assert_equal full_title("Help"), "Help | Ruby On Rails App"
end
end
What do these errors mean, and how do I go about fixing the errors?
NoMethodError: undefined method 'full_title' for #<SiteLayoutTest:0x00000006cee338>
test/integration/site_layout_test.rb:13
On line 13, you called a method that doesn't exist.
See http://ruby-doc.org/core-2.2.0/NoMethodError.html
ArgumentError: wrong number of arguments (0 for 1)
app/helpers/application_helper.rb:2:in 'full_title'
test/helpers/application_helper_test.rb:5
On line 5, you need to pass exactly one argument.
See http://ruby-doc.org/core-2.2.0/ArgumentError.html
See where the line numbers are in the error message?
I recommend reading an introductory ruby book, like http://ruby-doc.com/docs/ProgrammingRuby/ before (or while) learning rails.
Good luck!
In case anyone comes across the same error when they are going through the Michael Hartl 3rd edition...
I found the error in the app/helpers/application_helper.rb code. I needed to add an empty string for page_title, changing 'def full_title(page_title)' to 'def full_title(page_title= '')'
module ApplicationHelper
def full_title(page_title = '')
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
"#{page_title} | #{base_title}"
end
end
end