5:最初のテスト

rails generate controllerを実効した時点でテストが出来ている
$ ls test/controllers/
static_pages_controller_test.rb
見てみよう
$ 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
$
rails test
Running 2 tests in a single process (parallelization threshold is 50)
Run options: –seed 6539
# Running:
..
Finished in 0.506600s, 3.9479 runs/s, 3.9479 assertions/s.
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

db/test.sqlite3が出来ているので、Gitリポジトリに登録されると邪魔なのですが、.gitignoreに既にイグノアーされる記述が入っている
.gitignoreファイルの抜粋
# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-*
テスト駆動開発のサイクルは「失敗するテストを最初に書く」「次にアプリケーションのコードを書いて成功させる(パスさせる)」「必要ならリファクタリングする」のように進みます。多くのテストツールでは、テストの失敗を red 、成功したときを green で表します。ここから、このサイクルを「 redgreen ・REFACTOR」と呼ぶこともあります。これに従って最初のサイクルを完了させましょう。まず失敗するテストを書いて red になるようにします。
$ gedit 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
$ rails test
前略
NameError: undefined local variable or method `static_pages_about_url’
中略
3 runs, 2 assertions, 0 failures, 1 errors, 0 skips
エラーを見るとstatic_pages_about_urlのURLが無いと言っているのでabout用のルーティングを追加する
$ gedit test/controllers/static_pages_controller_test.rb
Rails.application.routes.draw do
 get ‘static_pages/home’
 get ‘static_pages/help’
 get “static_pages/about”   ←これを追記
root “application#hello”
end
そしてテスト
$ rails test
前略
AbstractController::ActionNotFound: The action ‘about’ could not be found for StaticPagesController
中略
3 runs, 2 assertions, 0 failures, 1 errors, 0 skips
今度のエラーはaboutのコントローラーが無いと言っているので
$ gedit app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
  def home
  end

  def help
  end

  def about   ← aboutを追記
  end
end
そしてテスト
$ rails test
1) Error:
StaticPagesControllerTest#test_should_get_about:
ActionController::MissingExactTemplate: StaticPagesController#about is 3 runs, 2 assertions, 0 failures, 1 errors, 0 skips
今度はaboutのテンプレートが無いといっているので
$ gedit 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=“https://rubyonrails.org/”>Ruby on Rails</a>.
 This is the sample app for the tutorial.
</p>
で、テスト
$ rails test
前略
3 runs, 3 assertions, 0 failures, 0 errors, 0 skips
無事エラーがゼロになった。そしてブラウザにhttp://localhost:3000/static_pages/aboutと入力すると

と表示された。

フジマル について

1947年生れ、東京電機大学二部電気通信工学科卒、最後はスリランカ航空で営業だったのですが2018年に㈱インパック・ジャパンに再就職。趣味:登山、スキー、車いじり、コンピューター
カテゴリー: その他 パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください