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?

お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

laravel PHPUnit assertDatabaseHusはnot nullのチェックができない

Last updated at Posted at 2024-07-14

概要

laravelのテストのおいて、テーブルのnot null系のチェック方法をまとめる

内容

一見 assertDatabaseHusを使えば下記のようにチェックできそうな気がする。
しかしながらそんなことはなく普通にエラーになる。(はず)

$this->assertDatabaseHas(
    table: 'reset_emails',
    data: [
        'email' => $email,
        'verify_code' => ['<>', null],
        'limit_at' => $expectedLimitAt,
    ]
);

下記のように実際にクエリを走らせてその結果のboolをassertTrueでチェックするとうまく確認できる。

// assertDatabaseHasだとnot nullのチェックができないためクエリでチェック
$exists = DB::table('reset_emails')
    ->where('email', $email)
    ->whereNotNull('verify_code',)
    ->where('limit_at', $expectedLimitAt)
    ->exists();
$this->assertTrue($exists);
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?