LoginSignup
0
0

More than 1 year has passed since last update.

railsチュートリアル 第3章 テスト駆動

Last updated at Posted at 2021-08-16

テストから始める

 自動化テストを作成して、機能が正しく実装されたことを確認する習慣をつける。
 アプリケーションを開発しながらテストスイート(Test Suite) をみっちり作成しておけば、いざというときのセーフティネットにもなる。
 テストを作成するということは、その分コードを余分に書くことになりますが、正しく行えば、むしろテストがないときよりも確実に開発速度がアップします。
 コントローラテスト、モデルテスト、統合テストの3つの種類がある。
 統合テストは、とりあえず強いらしい。
(ユーザーがWebブラウザでアプリケーションとやりとりする操作をシミュレートできるので特に強力です。)

最初のテスト

genelateで自動生成された static_pages_controller_test.rb を見てみる。

require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest
  test "should get home" do
    get static_pages_home_url
    # getはGETリクエストで受け付ける普通のWebページを示す
    # GETを受け付けるよね?
    assert_response :success
    # response:successはリクエストは成功し、レスポンスとともに要求に応じた情報が返される
    # GETリクエストが成功、ページが表示される。
    # response:successは、実際にはHTTPのステータスコード200 OKを指す
  end

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

end

文法をいきなり理解する必要はありません。
今は「このファイルにはテストが2つ書かれている」ことを認識していただければ十分です。

HomeとHelpに対応して生成されたものです
テストでは、アクションをgetして正常に動作することを確認
確認は「アサーション」(assertion: 主張、断言)と呼ばれる手法
getは、HomeページやHelpページが普通のWebページであるということを示します
response:success」は、実際にはHTTPのステータスコード200 OKを指す

ubuntu:~/environment/sample_app (static-pages) $ rails db:migrate
ubuntu:~/environment/sample_app (static-pages) $ rails t
Running via Spring preloader in process 5553
Run options: --seed 63601

# Running:

..

Finished in 6.931414s, 0.2885 runs/s, 0.2885 assertions/s.
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

Red

テスト駆動開発のサイクルは
「失敗するテストを最初に書く」
「次にアプリケーションのコードを書いて成功させる(パスさせる)」
「必要ならリファクタリングする」
のように進みます。
Redは失敗、Greenは成功。

require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest
  test "should get home" do
    get static_pages_home_url
    # getはGETリクエストで受け付ける普通のWebページを示す
    # GETを受け付けるよね?
    assert_response :success
    # response:successはリクエストが成功し、レスポンスとともに要求に応じた情報が返される
    # GETリクエストが成功、ページが表示されたらいい。
    # response:successは、実際にはHTTPのステータスコード200 OKを指す
  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
    # aboutページを表示させる(GETリクエスト)
    assert_response :success
    # GETリクエストが成功したかを確認

この場合aboutページがないから失敗Redを表示させると思う。

失敗

ubuntu:~/environment/sample_app (static-pages) $ rails t
Running via Spring preloader in process 3964
Run options: --seed 6271

# Running:

..E

Error:
StaticPagesControllerTest#test_should_get_about:

NameError: undefined local variable or method `static_pages_about_url' for #<StaticPagesControllerTest:0x00005648fc9cdee8>
    test/controllers/static_pages_controller_test.rb:20:in `block in <class:StaticPagesControllerTest>'


rails test test/controllers/static_pages_controller_test.rb:19



Finished in 1.572762s, 1.9075 runs/s, 1.2716 assertions/s.
3 runs, 2 assertions, 0 failures, 1 errors, 0 skips

テストのtest_should_get_aboutがおかしいらしい。
test_should_get_about_urlが見つからない。
多分 aboutページが存在していないからではないかと思う。
つまり aboutページを作ればいいと思う。

Rails.application.routes.draw do
  get 'static_pages/home'
  # static_pagesコントローラからhomeアクションに紐付けされる
  # getでアクセルすることでページを取得することができる
  get 'static_pages/help'
# 何を書いているかはわからない
   get 'static_pages/about'
   # aboutアクションにGETリクエストを送る
  root 'application#hello'
  # rootは流れだったような気がする
  # applicationコントローラのhelloアクションを起こす
end

aboutアクションにGETリクエストを送信する。
しかしaboutアクションが無いからまたエラーを起こすと思う。

ubuntu:~/environment/sample_app (static-pages) $ rails t
Running via Spring preloader in process 5789
Run options: --seed 37244

# Running:

..E

Error:
StaticPagesControllerTest#test_should_get_about:
AbstractController::ActionNotFound: The action 'about' could not be found for StaticPagesController
    test/controllers/static_pages_controller_test.rb:20:in `block in <class:StaticPagesControllerTest>'


rails test test/controllers/static_pages_controller_test.rb:19



Finished in 1.584873s, 1.8929 runs/s, 1.2619 assertions/s.
3 runs, 2 assertions, 0 failures, 1 errors, 0 skips

AbstractController::Actionでコントローラのアクションがおかしいらしい。
StaticPagesControllerの中にaboutアクションが見つからないらしい。
これからaboutアクションを作成

class StaticPagesController < ApplicationController
  def home
  end

  def help
  end

  def about
  # aboutアクションを作成
  end
end
ubuntu:~/environment/sample_app (static-pages) $ rails t
Running via Spring preloader in process 3597
Run options: --seed 6824

# Running:

..E

Error:
StaticPagesControllerTest#test_should_get_about:
ActionController::MissingExactTemplate: StaticPagesController#about is missing a template for request formats: text/html
    test/controllers/static_pages_controller_test.rb:20:in `block in <class:StaticPagesControllerTest>'


rails test test/controllers/static_pages_controller_test.rb:19



Finished in 1.365612s, 2.1968 runs/s, 1.4645 assertions/s.
3 runs, 2 assertions, 0 failures, 1 errors, 0 skips

MissingExactTemplate: StaticPagesController テンプレートが見つからないらしい。
about is missing a template for request formats: text/html 多分aboutのページを作成すれば成功か?
aboutページを作成するために新規ファイルを作成。
touchで新規ファイルを作成するみたい。

touch 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 application for the tutorial.
</p>
ubuntu:~/environment/sample_app (static-pages) $ rails t
Running via Spring preloader in process 4726
Run options: --seed 2251

# Running:

...

Finished in 2.743285s, 1.0936 runs/s, 1.0936 assertions/s.
3 runs, 3 assertions, 0 failures, 0 errors, 0 skips

成功。

プレビューでも成功を確認。

困ったこと

ubuntu:~/environment/sample_app (static-pages) $ rails t
/home/ubuntu/environment/sample_app/config/routes.rb:8:in `block in <main>': undefined local variable or method ` ' for #<ActionDispatch::Routing::Mapper:0x0000555ea56ae8a8> (NameError)
        from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.3/lib/action_dispatch/routing/route_set.rb:426:in `instance_exec'
        from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-
.

.

.

2.6.3/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from -e:1:in `<main>'

rails tを行なった。
しかし文字の羅列がたくさん出来た。
全角のスペースを消したらエラーが治った。

buntu:~/environment/sample_app (static-pages) $ git status
On branch static-pages
Your branch is up to date with 'origin/static-pages'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   app/controllers/static_pages_controller.rb
        modified:   config/routes.rb
        modified:   test/controllers/static_pages_controller_test.rb

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        app/views/static_pages/about.html.erb
        db/schema.rb

no changes added to commit (use "git add" and/or "git commit -a")

意味が全然わからない。しかし
modifiedは変更という意味らしい。

modified:   app/controllers/static_pages_controller.rb
modified:   config/routes.rb
modified:   test/controllers/static_pages_controller_test.rb

これらが変更点らしい。

git logを終了させる方法がqを入力

git pushのためパスワードを入力したらこんなメッセージが届いた

remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
fatal: unable to access 'https://github.com/アカウント名/レポジトリ名.git/': The requested URL returned error: 403
https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/

を見ろとかてあるように思えた。
和訳させるとトークンを作れと書いてあった。 サイトの説明に沿って作成。
トークン作成後プッシュのパスワードの代わりにトークンを入力、プッシュに成功
よかった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0