🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 5.6 练习 电子书中有练习的答案,如果想阅读参考答案,请[购买电子书](http://railstutorial-china.org/#purchase)。 避免练习和正文冲突的方法参见[3.6 节](chapter3.html#mostly-static-pages-exercises)中的说明。 1. 按照 [5.2.2 节](#syntactically-awesome-stylesheets)的建议,自己动手把底部的样式由[代码清单 5.13](#listing-footer-css) 中的 CSS 改写成[代码清单 5.15](#listing-refactored-scss) 中的 SCSS。 2. 在[代码清单 5.25](#listing-layout-links-test) 所示的集成测试中添加一些代码,使用 `get` 方法访问“注册”页面,确认这个页面有正确的标题。 3. 像[代码清单 5.36](#listing-test-helper-application-helper) 那样在测试辅助文件中引入应用的辅助方法后,能在测试中使用 `full_title` 辅助方法。然后可以使用[代码清单 5.37](#listing-layout-links-test-signup-full-title-helper) 中的测试检查页面的标题(在前一个练习的基础上编写)。不过这么做有个缺点,如果标题的公共部分有错别字(例如“Ruby on Rails Tutoial”),测试组件不能发现。所以要为 `full_title` 辅助方法编写一个测试:新建一个测试,专门用来测试应用的辅助方法,然后把[代码清单 5.38](#listing-full-title-helper-tests) 中的 `FILL_IN` 改成具体的代码。([代码清单 5.38](#listing-full-title-helper-tests) 使用 `assert_equal` 方法通过 `==` 操作符检查两个值是否相等。) ##### 代码清单 5.36:在测试中引入应用的辅助方法 test/test_helper.rb ``` ENV['RAILS_ENV'] ||= 'test' . . . class ActiveSupport::TestCase fixtures :all include ApplicationHelper . . . end ``` ##### 代码清单 5.37:在测试中使用 `full_title` 辅助方法 GREEN test/integration/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 signup_path assert_select "title", full_title("Sign up") end end ``` ##### 代码清单 5.38:测试 `full_title` 辅助方法 test/helpers/application_helper_test.rb ``` require 'test_helper' class ApplicationHelperTest < ActionView::TestCase test "full title helper" do assert_equal full_title, FILL_IN assert_equal full_title("Help"), FILL_IN end end ```