问题描述
我正在尝试理解第 5 章的最后一个练习Hartl 的 Rails 教程,但并不真正理解它,也不了解它要做什么.我的理由:
I'm trying to make sense of the last exercise in Chapter 5 of Hartl's Rails Tutorial but don't really understand it nor what it strives to do. My rationale:
它说的是full_title helper",指的是我们放在 app/controllers/helpers/application_helper.rb 中的代码:
It speaks of "full_title helper", which refers to the code we placed in app/controllers/helpers/application_helper.rb:
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
练习是:1) 通过将 "include ApplicationHelper"
行添加到/test/test_helper.rb
The exercise is:1) Include the above application_helper.rb file in the test_helper by adding the line "include ApplicationHelper"
to /test/test_helper.rb
2) 然后我们可以扩展 test/integration/site_layout_test.rb:
2) We can then extend test/integration/site_layout_test.rb with:
get signup_path
assert_select "title", full_title("Sign up")
使用 full_title 方法,注册"作为 page_title.
which uses the full_title method with "Sign up" as page_title.
但是,此测试不会测试例如 full_title 方法中 base_title 中的拼写错误.
However, this test would not test for for example typos in the base_title within the full_title method.
3) 分配:通过创建一个测试助手test/helpers/application_helper_test.rb"来解决这个限制.test/helpers/application_helper_test.rb 的解决方案代码:
3) Assignment: Solve this limitation by creating a test helper "test/helpers/application_helper_test.rb" that tests this.Solution code for test/helpers/application_helper_test.rb:
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
至少在我的情况下,结果是上面的完整标题助手测试仅在运行bundle exec rake test"时执行,而不是在运行bundle exec rake test:integration"时执行.但是为了运行bundle exec rake test",我们已经有一个很好的工作测试来检查每个页面的标题;我们以前做过.我虽然这个练习想要实现的是我们在运行bundle exec rake test:integration"时还测试了页面标题.
The result at least in my case is that the above full title helper test is only executed when running "bundle exec rake test" and not when running "bundle exec rake test:integration". But for running "bundle exec rake test" we already had a fine working test that checks each page title; we made that previously. What I though this exercise wanted to achieve that we also test for the page title when running "bundle exec rake test:integration".
我错过了什么?
推荐答案
如果你把测试文件保存在 test/helpers 下,那么使用:
If you saved the test file under test/helpers, then use:
rails test:helpers
这将运行该目录中的所有测试.
This will run all tests in that directory.
这篇关于Hartl Rail 教程:第 5 章,练习 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!