#author("2018-07-02T15:56:59+09:00","default:wikiwriter","wikiwriter") &tag(Railsテスティングガイド); *目次 [#b63ae818] #contents *関連ページ [#l728a547] -[[Rails テスティングガイド | Rails ガイド:https://railsguides.jp/testing.html]]…日本語版サイト -[[Testing Rails Applications — Ruby on Rails Guides:http://guides.rubyonrails.org/testing.html]]…公式サイト。日本語版は情報が古いのでこちらを参考にすること。 *参考情報 [#z8d31c99] * 1 Railsアプリケーションでテストを作成しなければならない理由 [#m7e16995] * 2 テストを導入する [#xe4e9d82] ** 2.1 Rails Sets up for Testing from the Word Go [#j52f7a32] -Railsプロジェクトを作成すると以下のようなフォルダが作成される #pre{{ $ ls -F test controllers/ helpers/ mailers/ system/ test_helper.rb fixtures/ integration/ models/ application_system_test_case.rb }} ** 2.2 The Test Environment [#v67cb360] -testはtest環境で実施される。 -config/database.ymlでテスト用のデータベースが設定できる。 ** 2.3 Rails meets Minitest [#ye73ffcd] -モデルを生成するとテストが自動的に作成される。 #pre{{ $ bin/rails generate model article title:string body:text ... create app/models/article.rb create test/models/article_test.rb create test/fixtures/articles.yml ... }} -test/models/article_test.rbは以下のような内容となる。 #pre{{ require 'test_helper' class ArticleTest < ActiveSupport::TestCase # test "the truth" do # assert true # end end }} -テストメソッド名は以下のどちらでもよい。 #pre{{ test "the truth" do assert true end def test_the_truth assert true end }} -失敗テスト。articleのtitleが設定されていないので以下のテストは失敗する。 #pre{{ test "should not save article without title" do article = Article.new assert_not article.save end }} -実行。本来失敗してほしいのだが成功してしまう。 #pre{{ $ bin/rails test test/models/article_test.rb }} -articleに検証メソッドを追加すると成功する。 #pre{{ class Article < ApplicationRecord validates :title, presence: true end }} ** 2.4 Available Assertions [#fbdc2b74] **2.5 Rails Specific Assertions [#se6208f2] **2.6 A Brief Note About Test Cases [#ib167b69] **2.2 Railsを即座にテスト用に設定する [#qb174f1a] -プロジェクトを作成すると、以下のフォルダが自動的に作成される。 #pre{{ $ ls -F test controllers/ helpers/ mailers/ test_helper.rb fixtures/ integration/ models/ }} **2.3 フィクスチャのしくみ [#e38e1008] -フィクスチャ=サンプルデータ。 -YAMLで記述。test/fixtures以下。 #pre{{ david: name: David Heinemeier Hansson birthday: 1979-10-15 profession: Systems development steve: name: Steve Ross Kellock birthday: 1974-09-27 profession: guy with keyboard }} -YAMLはERBで処理される。 -フィクスチャはActive Recordオブジェクト。 #pre{{ # davidという名前のフィクスチャに対応するUserオブジェクトを返す users(:david) # idで呼び出されたdavidのプロパティを返す users(:david).id # Userクラスで利用可能なメソッドにアクセスすることもできる email(david.girlfriend.email, david.location_tonight) }} -フィクスチャとして容易したymlファイルはテストの実行前にテストDBにロードされる。 *3 モデルに対する単体テスト [#c65fc1b9] -articleモデルを生成。 #pre{{ $ bin/rails generate scaffold article title:string body:text }} -以下のようなテストクラスが生成される。 #pre{{ require 'test_helper' class ArticleTest < ActiveSupport::TestCase # test "the truth" do # assert true # end end }} **3.1 テストデータベースのスキーマを管理する [#re44c079] **3.2 テストを実行する。 [#pfc34540] -以下のように実行できる。 #pre{{ bin/rails test test/models/article_test.rb }} -特定のメソッドdけをテスト。 #pre{{ bin/rails test test/models/article_test.rb test_the_truth }} -失敗のテスト。article.rbを変更。 #pre{{ class Article < ActiveRecord::Base validates :title, presence: true end }} -テスト追加。 #pre{{ class ArticleTest < ActiveSupport::TestCase test "the truth" do assert true end test "should not save article without title" do article = Article.new assert_not article.save, "Saved the article without a title" end end }} **3.3 単体テストに含めるべき項目 [#j3ce5ae7] **3.4 利用可能なアサーション [#g83e6cdb] **3.5 Rails固有のアサーション [#y9342428] *4 コントローラの機能テスト [#t3c058fd] -1つのコントローラーに含まれる複数のアクションをテスト。 -機能テスト=functional test。 **4.1 機能テストに含める項目 [#y660cd93] -以下の項目を含める --Webリクエストが成功したか --正しいページにリダイレクトされたか --ユーザー認証が成功したか --レスポンスのテンプレートに正しいオブジェクトが保存されたか --ビューに表示されたメッセージは適切か -test/controllers/articles_controller_test.rb #pre{{ test "should get new" do get new_article_url assert_response :success end }} -showアクションに対するテストの場合。 #pre{{ test "should show article" do get article_url(@article) assert_response :success end }}