【37日目】【1日20分のRailsチュートリアル】【第5章】リンクのテストとUserコントローラの作成
今日は「5.3.4 リンクのテスト」から。
5.3.4 リンクのテスト
レイアウト内のいくつかのリンクを埋めることができたので、これらのリンクが正しく動いているかどうかチェックするテストを書いてみましょう。
リンクもテストできるのか。いいね!
$ rails generate integration_test site_layout
さっき作ったファイルにテストコードを追加。
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 end end
例えばAboutページのリンク(<a href="/about">...</a>
)があるかどうかテストするには
assert_select "a[href=?]", about_path
と書くらしい。
count:
をつけることで個数をチェックすることも可能。
色々できますなぁ。
assert_select "a[href=?]", root_path, count: 2
テストが通るか確認。
リンクのテストが通ったらすべてのテストも実行。
$ bundle exec rake test:integration 1 tests, 5 assertions, 0 failures, 0 errors, 0 skips $ bundle exec rake test 5 tests, 13 assertions, 0 failures, 0 errors, 0 skips
テスト通りました。
5.4 ユーザー登録: 最初のステップ
Webアプリケーションに必須と言っても過言ではないユーザー登録!
大事ですね。
5.4.1 Usersコントローラ
newアクションを持つUserコントローラを作成。
$ rails generate controller Users new
create app/controllers/users_controller.rb
route get 'users/new'
invoke erb
create app/views/users
create app/views/users/new.html.erb
invoke test_unit
create test/controllers/users_controller_test.rb
invoke helper
create app/helpers/users_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/users.coffee
invoke scss
create app/assets/stylesheets/users.scss
ビューとかちょっとしたテストも自動生成されるらしい。
テスト実行して現時点で問題ないことを確認。
$ bundle exec rake test 6 tests, 14 assertions, 0 failures, 0 errors, 0 skips
今日の作業時間は【24分】。
次は「5.4.2 ユーザー登録用URL」から。