The Andela Way
Published in

The Andela Way

Mock Testing in Laravel

Photo by Pankaj Patel on Unsplash
The outline for mocking in Laravel official docs.
<env name=”DB_CONNECTION” value=”sqlite”/><env name=”DB_DATABASE” value=”:memory:”/>
Bus::fake();
$user = factory(User::class)->create();
$this->get('/example/job');
Bus::assertDispatched(ExampleJob::class, function ($job) use ($user) {
return $job->user->id === $user->id;
});
vendor/bin/phpunit --filter bus_fake_test
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: GET http://localhost/example/job
Route::get('/example/job', function () {});
The expected [App\Jobs\ExampleJob] job was not dispatched.
Failed asserting that false is true.
“php artisan make:job ExampleJob”
Route::get('/example/job', function () {
$user = \App\User::first();
\App\Jobs\ExampleJob::dispatch($user);
});
assertDispatched($command, $callback = null)
assertDispatchedTimes($command, $times = 1)
assertNotDispatched($command, $callback = null)
dispatched($command, $callback = null)
hasDispatched($command)
dispatch($command)
dispatchNow($command, $handler = null) etc.

--

--

A pool of thoughts from the brilliant people at Andela

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aliu Wahab

I ARCHITECT and WRITE Web Apps and API Services: => PHP (Laravel/Lumen), JavaScript (Vue.js).