Closed3
個人アプリ用のminitest

test/fixtures
について調べる。
スキャフォで作成されたファイルにここを読めと書いてあった。
◆ メモ
◇ 1モデル1ファイル。
◇ 書き方はこんな感じ。
識別するための名前:
属性: 値
属性: 値
属性: 値
◇ レコードの順序を指定したい場合omap YAML typeを使う。
--- !omap
- parent:
id: 1
parent_id: nil
name: 親
- child:
id: 2
parent_id: 1
name: 子
◇ テスト毎の流れ
- test用dbからfixturesのデータ削除
- test用dbにfixturesのデータ追加
◇ test/fixtures/*yml
にはERBを書くことができる。
◇ test/fixtures/a.yml
に書いたメソッドは、test/fixtures/b.yml
で使うことはできない。
test/fixtures/a.yml
とtest/fixtures/b.yml
の両方で使うことができるメソッドは、
次の手順で用意。
-
test/test_helper.rb
にモジュールを書いて、 -
ActiveRecord::FixtureSet.context_class
にincludeする。
module Hoge
def pom
'aaa'
end
end
ActiveRecord::FixtureSet.context_class.include Hoge
◇ 関連付け
belongs_toの表現に外部キーを数字ではなく識別子で指定することができる。
# user.yml
tarou:
name: tarou
gender: man
# order.yml ( before )
potate
price: 150
user_id: 1 # <= これに注目
# order.yml ( after )
potate
price: 150
user_id: tarou # <= これに注目
Polymorphic, has_and_belongs_to_many, has_many :throughの書き方も書いてあった。
気になる人は調べてください。
◇ timestampは自動補完されるらしい。
created_at, created_on, updated_at, updated_onには何も指定しない場合、
現在時刻が追加される。
◇ $LABEL
ラベルを使いまわすことができる。
# user.yml (before)
tarou:
name: tarou
gender: man
# user.yml ( after )
tarou:
name: $LABEL
gender: man
◇ 使い回し方法が紹介されていた。
一旦は使い回す必要がないから飛ばす。
気になる人は調べてください。
DEFAULTS
, _fixture.ignore

deviseを使っている場合、
テストの途中でログインする必要がある。
ログインは次の手順
class HogesControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test 'yahho' do
# sign_in fixtureのファイル名(フィクスチャのラベル名)
sign_in users(:tarou)
end
end
このスクラップは2ヶ月前にクローズされました