ぞえの技術めも

Ruby on Rails勉強中

【117日目】【1日20分のRailsチュートリアル】【第9章】演習の2.

Ruby on Railsチュートリアル(第3版)

今日は「9.6 演習」の2.から。

9.6 演習

2.レイアウトにあるすべてのリンクに対して統合テストを書いてみましょう。
ログイン済みユーザーとそうでないユーザーのそれぞれに対して、正しい振る舞いを考えてください。
ヒント: log_in_asヘルパーを使ってリスト5.25にテストを追加してみましょう。

リスト5.25でテストしているのはHome、Help、About、Contactへのリンクについて。
Log inのリンクとかはログインの有無によってリンクが変わるのでその辺のテストを書けばいいのかな。

test/integration/site_layout_test.rb

class SiteLayoutTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:michael)
  end

  test "layout links" do
    :
    assert_select "a[href=?]", login_path
    :
  end

  test "layout links at login" do
    log_in_as(@user)
    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=?]", users_path
    assert_select "a[href=?]", user_path(@user)
    assert_select "a[href=?]", edit_user_path(@user)
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", about_path
    assert_select "a[href=?]", contact_path
  end
  :

元々あったテストにログインのリンクを確認するコードを追加して、
ログイン時のリンクを確認するテストも追加した。

$ bundle exec rake test
41 tests, 166 assertions, 0 failures, 0 errors, 0 skips

とりあえずテストは通ったし大丈夫かな。。。なにか足りてないかもしれないけど。

今日の学習時間は【23分】

次は「9.6 演習」の3.から。