Reputation: 57
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
<a href="http://www.railstutorial.org/help">Rails Tutorial help section</a>.
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>
Upvotes: 1
Views: 513
Reputation: 46
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
Upvotes: 1
Reputation: 10462
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
Upvotes: 1