在 Michael Hartl 的 Rails 教程的第 8 章,清单 8.25 测试套件 RED
On Chapter 8 of Michael Hartl's Rails Tutorial, Listing 8.25 test suite RED
我是 Whosebug 的新手,因为我最近开始使用 Michael Hartl 的教程在 Rails 上学习 Ruby。我似乎无法通过清单 8.25 的测试 ("At this point, the test suite should still be GREEN:" $ bundle exec rake test):
我的测试是RED,错误如下:
~/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
我把第7章和第8章的所有最近的变化都重新看了一遍; 8.20、8.21、8.24测试刚刚通过
错误中提到的文件 site_layout_test 如下所示:
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
添加:
1) 这是我正在做的教程:https://www.railstutorial.org/book/log_in_log_out
2) 这是错误所指的文件:
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
如果我去掉最后一个 assert_select,我就不会再收到错误了。
这是 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
非常感谢您的帮助。提前致谢!
我敢打赌,原因是您没有执行 Listing 5.37 中的步骤,您在 ApplicationHelper
中包含 test_helper.rb
:
class ActiveSupport::TestCase
fixtures :all
include ApplicationHelper
.
.
.
end
我是 Whosebug 的新手,因为我最近开始使用 Michael Hartl 的教程在 Rails 上学习 Ruby。我似乎无法通过清单 8.25 的测试 ("At this point, the test suite should still be GREEN:" $ bundle exec rake test):
我的测试是RED,错误如下:
~/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
我把第7章和第8章的所有最近的变化都重新看了一遍; 8.20、8.21、8.24测试刚刚通过
错误中提到的文件 site_layout_test 如下所示:
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
添加:
1) 这是我正在做的教程:https://www.railstutorial.org/book/log_in_log_out
2) 这是错误所指的文件:
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
如果我去掉最后一个 assert_select,我就不会再收到错误了。
这是 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
非常感谢您的帮助。提前致谢!
我敢打赌,原因是您没有执行 Listing 5.37 中的步骤,您在 ApplicationHelper
中包含 test_helper.rb
:
class ActiveSupport::TestCase
fixtures :all
include ApplicationHelper
.
.
.
end