0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

テストデータを作成する際に注意したいこと

Last updated at Posted at 2025-02-07
  • 対象のモデルのバリデーションを確実に把握する
  • テストデータを作る際だけバリデーションを無視できる設定がされている可能性がある
    • 例えば以下のように書くと、rsepcでテストデータを作成する場合、ルールが無視される
    • validate :name, unless: -> { validation_context == :fixture }
  • travel_to, let!, before などの記述順を正しく把握する事
  • 実運用でSQLからデータを登録していたとしても、アプリケーションから操作する際は、モデルのバリデーションを通るので、真っ先にモデルのバリデーションを調べにいくこと

tips

  • 要件によっては、updateで更新するだけじゃなくて、destroy!で消しちゃう手もある
    • 既存のデータを使い回す際に、データをdisabledにしなくて、消しちゃっても問題ないケースもある
  • 複数のデータの場合はどうなるのか?の観点は大事。単数と複数で大丈夫か確認した方が良い
  • デバッカーは色んな場所に仕込めるので工夫すると良い
    • 例えば、expectの前に仕込んで、subjectを実行する前と後を調べる
    • その処理の中で呼ばれるメソッドの中で仕込んで、途中のデータがどうなっているか調べる

など

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?