raihanrazon's avatar

raihanrazon wrote a reply+100 XP

4mos ago

If you are not using RefreshDatabase or anything to seed/refresh database every time during testing, you can follow this approach -

1- Create a boot() function in tests/Pest.php

/**
 * A generic boot function for Laravel applications.
 *
 * @return mixed
 */
function boot()
{
    $app = require __DIR__.'/../bootstrap/app.php';
    $app->make(Kernel::class)->bootstrap();
    restore_error_handler();
    restore_exception_handler();

    return $app;
}

2- Use the function inside your dataset before making a DB query

dataset('ids', function () {
    boot();
    return User::where('is_active', 1)->pluck('id')->toArray();
});

3- Use the dataset in your test.

test('data loading', function ($ids) {
    expect($detectableId)->toBeInt();
})->with('ids');