ほぼ静的なページの作成(その2)

開発スキルのないSIerからソフトウェアスキルを取得するため、WEDでモノづくりを計画しています。詳しい経緯はここ
※現在はruby on railsチュートリアル(ここから)を進めていってます。

前回の続きruby on rails チュートリアルの第3章に入っています。

テストから始める

何らかの変更を行う際には、常に「自動化テスト」を作成して、機能が正しく実装されたことを確認する習慣を身につけたいです。
特に今後railsアプリを作成していく上でデバッグするため、テスト作成の上達できるよう頑張ります!
前回はHomeページとHelpページを作成して中身も書き加えたので、今度はAboutページを同様に追加します。

最初のテスト

前回実施した"rails generate controller"コマンドを実施することでテストファイルが作成されています。

テストファイルがちゃんと作成されているので確認!!
確かに作成されていますねー!!

$ ls test/controllers/
static_pages_controller_test.rb

◆生成されたStaticPagesコントローラのデフォルトのテストを見てみる
言葉で表すと・・・「home(help)ページのテスト。GETリクエストをhomeアクションに対して発行 (=送信) せよ。そうすれば、リクエストに対するレスポンスは成功になるはず」です。それっぽい事が書いてあるのはなんとなくわかるのですが、実際自分でコードを書こうと思ったときにこんな書き方できるのかが不安・・・。まぁ現時点では詳しく理解する必要はないみたいです。
このファイルに”home”と”help”がファイルに記載されている事を確認できればOKとのことです!

$ cat test/controllers/static_pages_controller_test.rb 
require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest
  test "should get home" do
    get static_pages_home_url
    assert_response :success
  end

  test "should get help" do
    get static_pages_help_url
    assert_response :success
  end

end

◆StaticPagesコントローラのデフォルトのテスト green
デフォルトのままなのでrails testしても当然OKになるはず

$ rails test
:
:
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

→無事にテストOKになりました。

Aboutページの記載をしてテストしてNG(red)になることを確認してみる
static_pages_controller_test.rb にaboutの記載をしてテストする

$ cat test/controllers/static_pages_controller_test.rb 
require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest
  test "should get home" do
    get static_pages_home_url
    assert_response :success
  end

  test "should get help" do
    get static_pages_help_url
    assert_response :success
  end

  test "should get about" do
    get static_pages_about_url
    assert_response :success
  end
end

もう一度テストしてみる。がaboutのページなんて作ってないのでもちろんerror(Red)になります。

$ rails test
:
:
  1) Error:
StaticPagesControllerTest#test_should_get_about:
NameError: undefined local variable or method `static_pages_about_url' for #<StaticPagesControllerTest:0x007fcb5bf137a8>
    test/controllers/static_pages_controller_test.rb:15:in `block in <class:StaticPagesControllerTest>'

3 runs, 2 assertions, 0 failures, 1 errors, 0 skips

◆エラーが出たページを作ってみる
NameError: undefined local variable or method `static_pages_about_url' といったaboutっていうページのURLがないっていうことなので、解消してみます。
エラーを解消するために、about用のルートを追加します。rails generateしたときは自動で出来たけど、rails generateでaboutを指定したわけではないので自分でルートを設定してやるということですね。

$ cat config/routes.rb 
Rails.application.routes.draw do
  get 'static_pages/home'
  get 'static_pages/help'
  get  'static_pages/about'  #★ponta追加
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root 'application#hello'
end

そしてテストしてみます。

$ rails test
:
:
StaticPagesControllerTest#test_should_get_about:
AbstractController::ActionNotFound: The action 'about' could not be found for StaticPagesController
:
:

まだエラーが出るが内容が変わりました AbstractController::ActionNotFound: The action 'about' could not be found for StaticPagesController

◆StaticPagesコントローラにaboutを追加する
ルートだけじゃなくてcontrollerにも追加必要になるんですね。。。

$ cat app/controllers/static_pages_controller.rb 
class StaticPagesController < ApplicationController
  def home
  end

  def help
  end

  def about
  end
end

早速テストする

$ rails test
:
:
  1) Error:
StaticPagesControllerTest#test_should_get_about:
ActionController::UnknownFormat: StaticPagesController#about is missing a template for this request format and variant.

request.formats: ["text/html"]
request.variant: []

NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.
    test/controllers/static_pages_controller_test.rb:15:in `block in <class:StaticPagesControllerTest>'

3 runs, 2 assertions, 0 failures, 1 errors, 0 skips

まだまだエラーが出ますね。。チュートリアルトラブルシューティングを学ばせてくれますね!!

エラー内容が変わり今回はテンプレートがないよと言ってるそうです。ちなみにテンプレートはすなわちビューの事です。

◆テンプレートを作る
viとかvimとかでも出来そうなのでチュートリアルの著者はtouchコマンドで作成するのがかっこいいらしいです。
※正直よくわからんかったです・・・w

$ touch app/views/static_pages/about.html.erb

当然空ファイルのままではだめなので中身を書いていきます

$ cat app/views/static_pages/about.html.erb
<h1>About</h1>
<p>
  <a href="https://railstutorial.jp/">Ruby on Rails Tutorial</a>
  is a <a href="https://railstutorial.jp/#ebook">book</a> and
  <a href="https://railstutorial.jp/#screencast">screencast</a>
  to teach web development with
  <a href="http://rubyonrails.org/">Ruby on Rails</a>.
  This is the sample application for the tutorial.
</p>

んでテストしてみます

$ rails test
:
:
Finished in 1.124321s, 2.6683 runs/s, 2.6683 assertions/s.

3 runs, 3 assertions, 0 failures, 0 errors, 0 skips

よし、これでOK!!新たなページを作成するには、ルートを作ってテンプレートを作る事が必要なんですね!
めっちゃ勉強になりました。

今回はここまで!!次回からは3章の続きで少しだけ動的なページを作っていきます!では^^