#author("2018-07-02T14:29:28+09:00","default:wikiwriter","wikiwriter") &tag(Railsテスティングガイド); *目次 [#b63ae818] #contents *関連ページ [#l728a547] -[[Rails テスティングガイド | Rails ガイド:https://railsguides.jp/testing.html]]…公式サイト *参考情報 [#z8d31c99] * 1 Railsアプリケーションでテストを作成しなければならない理由 [#m7e16995] * 2 テストを導入する [#xe4e9d82] ** 2.1 test環境 [#o3502f1b] -config/database.ymlでテスト用のデータベースが設定できる。 **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) }} *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 }}