Exercises
撰寫整合測試,用 get 去瀏覽 Sign up 頁面,確認這個頁面有正確的標題
在 test/test_helper.rb 導入 ApplicationHelper,就能在測試使用 full_title 輔助方法:
ENV['RAILS_ENV'] ||= 'test'
.
.
.
class ActiveSupport::TestCase
fixtures :all
include ApplicationHelper
.
.
.
end
然後可以在 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
這樣做有個缺點,如果共用的標題有錯字,測試其實無法發現,所以可以為 full_title 輔助方法建立一個測試,專門用來測試 ApplicationHelper。
把下面的 FILL_IN 改成具體的程式碼:
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
assert_equal 方法透過 == 檢查兩個值是否相等。