Railsドキュメント

テストデータ

適応バージョン

説明

事前に用意したテストデータを読み込み常にDBの内容を一定に保つための仕組みのことをフィクスチャと呼ぶ

フィクスチャを用意

test/fixtures/テーブル名.yml

rubyonrails:
  id: 1
  name: Ruby on Rails
  url: http://www.rubyonrails.org

google:
  id: 2
  ;name: Google
  url: http://www.google.com

テスト内からフィクスチャを読み込む

require 'test_helper'

class WebSiteTest < ActiveSupport::TestCase
  test "web_site_count" do
    assert_equal 2, WebSite.count
  end
end

フィクスチャを使用

require 'test_helper'

class SiteTest < ActiveSupport::TestCase
  fixtures :sites

  def test_google_fixture
    # フィクスチャ名を指定して使用
    google = sites(:google)
    assert_equal "http://www.google.com", google.url
  end

  def test_google_fixture_from_db
    # findメソッドなどでDBからロードして使用
    google = Site.find(2)

    # フィクスチャ名で取得したものと同じであることをバリデーション
    assert_equal sites(:google), google.url
  end
end

フィクスチャからのデータの取得

フィクスチャに含まれているデータはテストの実行時に使うもの