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
Related
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
I'm new on StackOverflow as I've recently begun to learn Ruby on Rails with Michael Hartl's Tutorial. I can't seem to pass the test of listing 8.25 ("At this point, the test suite should still be GREEN:" $ bundle exec rake test):
My test is RED and the error is as follows:
~/workspace/sample_app (log-in-log-out) $ bundle exec rake test
Started
ERROR["test_layout_links", SiteLayoutTest, 2016-04-14 23:15:47 +0000]
test_layout_links#SiteLayoutTest (1460675747.92s)
NoMethodError: NoMethodError: undefined method `full_title' for # <SiteLayoutTest:0x0000000a30bad8>
test/integration/site_layout_test.rb:13:in `block in <class:SiteLayoutTest>'
test/integration/site_layout_test.rb:13:in `block in <class:SiteLayoutTest>'
22/22: [======================================================] 100% Time: 00:00:01, Time: 00:00:01
Finished in 1.30261s
tests, 49 assertions, 0 failures, 1 errors, 0 skips
I've gone through all the recent changes in chapter 7 and 8 again; the tests of 8.20, 8.21 and 8.24 just pass.
The file site_layout_test mentioned in the error looks like this:
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
ADDITIONS:
1) This is the tutorial I'm doing: https://www.railstutorial.org/book/log_in_log_out
2) This is the file that the error refers to:
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
If I take the last assert_select out, I don't get the error anymore.
And this is 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
Your help is much appreciated. Thanks in advance!
I would wager the reason is because you haven't performed the step in Listing 5.37, where you include in the ApplicationHelper in your test_helper.rb:
class ActiveSupport::TestCase
fixtures :all
include ApplicationHelper
.
.
.
end
When I run bundle exec rake test it comes up with an error in the terminal. It can't seem to get passed this step
# Running:
.......EEE.......
Finished in 0.420936s, 40.3862 runs/s, 61.7672 assertions/s.
1) Error:
StaticPagesControllerTest#test_should_get_help:
NameError: uninitialized constant ApplicationController::TestCase
app/controllers/static_pages_controller.rb:1:in `<top (required)>'
2) Error:
StaticPagesControllerTest#test_should_get_home:
NameError: uninitialized constant ApplicationController::TestCase
app/controllers/static_pages_controller.rb:1:in `<top (required)>'
3) Error:
StaticPagesControllerTest#test_should_get_about:
NameError: uninitialized constant ApplicationController::TestCase
app/controllers/static_pages_controller.rb:1:in `<top (required)>'
17 runs, 26 assertions, 0 failures, 3 errors, 0 skips
I tried fixing the problem to the code but have not come up with a solution.
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get :home
assert_response :success
assert_select "title", "Home | 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
end
The first line of you test code should be:
class StaticPagesControllerTest < ActionController::TestCase
It's ActionController - not ApplicationController.
hi i am presently on chapter 8 micheal hartl Learning Rails 3rd edition.
presently i am stucked in chapter 8 with above errors .I will highly appreciate if somebuddy help me out with it. Thanks in advance
when i run bundle exec rake test it comes up with
sample_app git:(log-in-log-out) ✗ bundle exec rake test
Run options: --seed 16515
# Running:
......E............
Finished in 0.944425s, 20.1181 runs/s, 40.2361 assertions/s.
1) Error:
SiteLayoutTest#test_layout_links:
NoMethodError: undefined method `full_title' for #<SiteLayoutTest:0x007ff8bea694b0>
test/integration/site_layout_test.rb:12:in `block in <class:SiteLayoutTest>'
19 runs, 38 assertions, 0 failures, 1 errors, 0 skips
whereas my file posses the following
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
full_title is a helper method defined in application_helper.rb. Check out https://www.railstutorial.org/book/_single-page#code-title_helper
I'm following along with RailsSpace: Building a Social Networking Website with Ruby on Rails by Michael Hartl. Running rails v2.3.2.
I've gotten to the 5th chapter in which testing is introduced. The following is supposed to match the title of the various pages to strings using the get method:
require File.dirname(__FILE__) + '/../test_helper'
require 'site_controller'
# Re-raise errors caught by the controller.
class SiteController; def rescue_action(e) raise e end; end
class SiteControllerTest < Test::Unit::TestCase
def setup
#controller = SiteController.new
#request = ActionController::TestRequest.new
#response = ActionController::TestResponse.new
end
def test_index
get :index
title = assigns(:title)
assert_equal "Welcome to RailsSpace!", title
assert_response :success
assert_template "index"
end
def test_about
get :title
title = assigns(:title)
assert_equal "About RailsSpace", title
assert_response :success
assert_template "about"
end
def test_help
get :help
title = assigns(:title)
assert_equal "RailsSpace Help", title
assert_response :success
assert_template "help"
end
end
On compiling I get:
Loaded suite site_controller_test
Started
EEE
Finished in 0.057 seconds.
1) Error:
test_about(SiteControllerTest):
NoMethodError: undefined method `get' for #<SiteControllerTest:0x4854b30>
site_controller_test.rb:23:in `test_about'
2) Error:
test_help(SiteControllerTest):
NoMethodError: undefined method `get' for #<SiteControllerTest:0x4854b1c>
site_controller_test.rb:31:in `test_help'
3) Error:
test_index(SiteControllerTest):
NoMethodError: undefined method `get' for #<SiteControllerTest:0x485470c>
site_controller_test.rb:15:in `test_index'
3 tests, 0 assertions, 0 failures, 3 errors
Other people have had this issue and the only proposed solution is just to reinstall. I'm not to enthused by this. Since this is an older book there this is probably just breakage between rails versions. What would be the equivalent of this for rails v2.3.2?
Replace all the following code
# Re-raise errors caught by the controller.
class SiteController; def rescue_action(e) raise e end; end
class SiteControllerTest < Test::Unit::TestCase
def setup
#controller = SiteController.new
#request = ActionController::TestRequest.new
#response = ActionController::TestResponse.new
end
with
class SiteControllerTest < ActionController::TestCase
The code you are using refers to Rails 2.0/2.1.
Try changing Test::Unit::TestCase to ActionController::TestCase.
One other thing you might like to know is that Railspace has evolved into Insoshi so the latest code is available there. Might be handy for you when you run into other issues.