Personally I never mock a db call but rather hit the database when I need to. For that I use an in memory sqlite database. The tests might be a tiny bit slower but I if running them in parallel it's hardly noticable.
Oct 21, 2021
6
Level 12
Mocking facade DB query builder `when`
Hi, I am using laravel mocking of facade to unit test my query. but I cant continue because i dont know how to create a mock for when. does anyone here have experience doing it? see code below for the example..
DB::table('users')
->join('profiles', 'profiles.user_id', 'users.id')
->when(true, function ($q) {
$q->where('profiles.age', '>', 20);
})->get();
below is my test so far..
DB::shouldReceive('table')
->once()
->with('users')
->andReturnSelf();
DB::shouldReceive('join')
->once()
->with('profiles', 'profiles.user_id', 'users.id')
->andReturnSelf();
but i can't continue further because of when i don't know how to create a mock for when and I can't find any resources online. I hope someone can help me with this. thank you
Please or to participate in this conversation.