Mocking
- Introduction
- Mocking Objects
- Mocking Facades
- Bus Fake
- Event Fake
- HTTP Fake
- Mail Fake
- Notification Fake
- Queue Fake
- Storage Fake
- Interacting With Time
Introduction
When testing Laravel applications, you may wish to "mock" certain aspects of your application so they are not actually executed during a given test. For example, when testing a controller that dispatches an event, you may wish to mock the event listeners so they are not actually executed during the test. This allows you to only test the controller's HTTP response without worrying about the execution of the event listeners since the event listeners can be tested in their own test case.
Laravel provides helpful methods for mocking events, jobs, and other facades out of the box. These helpers primarily provide a convenience layer over Mockery so you do not have to manually make complicated Mockery method calls.
Mocking Objects
When mocking an object that is going to be injected into your application via Laravel's service container, you will need to bind your mocked instance into the container as an instance
binding. This will instruct the container to use your mocked instance of the object instead of constructing the object itself:
1use App\Service;2use Mockery;3use Mockery\MockInterface;45public function test_something_can_be_mocked()6{7 $this->instance(8 Service::class,9 Mockery::mock(Service::class, function (MockInterface $mock) {10 $mock->shouldReceive('process')->once();11 })12 );13}
1use App\Service;2use Mockery;3use Mockery\MockInterface;45public function test_something_can_be_mocked()6{7 $this->instance(8 Service::class,9 Mockery::mock(Service::class, function (MockInterface $mock) {10 $mock->shouldReceive('process')->once();11 })12 );13}
In order to make this more convenient, you may use the mock
method that is provided by Laravel's base test case class. For example, the following example is equivalent to the example above:
1use App\Service;2use Mockery\MockInterface;34$mock = $this->mock(Service::class, function (MockInterface $mock) {5 $mock->shouldReceive('process')->once();6});
1use App\Service;2use Mockery\MockInterface;34$mock = $this->mock(Service::class, function (MockInterface $mock) {5 $mock->shouldReceive('process')->once();6});
You may use the partialMock
method when you only need to mock a few methods of an object. The methods that are not mocked will be executed normally when called:
1use App\Service;2use Mockery\MockInterface;34$mock = $this->partialMock(Service::class, function (MockInterface $mock) {5 $mock->shouldReceive('process')->once();6});
1use App\Service;2use Mockery\MockInterface;34$mock = $this->partialMock(Service::class, function (MockInterface $mock) {5 $mock->shouldReceive('process')->once();6});
Similarly, if you want to spy on an object, Laravel's base test case class offers a spy
method as a convenient wrapper around the Mockery::spy
method. Spies are similar to mocks; however, spies record any interaction between the spy and the code being tested, allowing you to make assertions after the code is executed:
1use App\Service;23$spy = $this->spy(Service::class);45// ...67$spy->shouldHaveReceived('process');
1use App\Service;23$spy = $this->spy(Service::class);45// ...67$spy->shouldHaveReceived('process');
Mocking Facades
Unlike traditional static method calls, facades (including real-time facades) may be mocked. This provides a great advantage over traditional static methods and grants you the same testability that you would have if you were using traditional dependency injection. When testing, you may often want to mock a call to a Laravel facade that occurs in one of your controllers. For example, consider the following controller action:
1<?php23namespace App\Http\Controllers;45use Illuminate\Support\Facades\Cache;67class UserController extends Controller8{9 /**10 * Retrieve a list of all users of the application.11 *12 * @return \Illuminate\Http\Response13 */14 public function index()15 {16 $value = Cache::get('key');1718 //19 }20}
1<?php23namespace App\Http\Controllers;45use Illuminate\Support\Facades\Cache;67class UserController extends Controller8{9 /**10 * Retrieve a list of all users of the application.11 *12 * @return \Illuminate\Http\Response13 */14 public function index()15 {16 $value = Cache::get('key');1718 //19 }20}
We can mock the call to the Cache
facade by using the shouldReceive
method, which will return an instance of a Mockery mock. Since facades are actually resolved and managed by the Laravel service container, they have much more testability than a typical static class. For example, let's mock our call to the Cache
facade's get
method:
1<?php23namespace Tests\Feature;45use Illuminate\Foundation\Testing\RefreshDatabase;6use Illuminate\Foundation\Testing\WithoutMiddleware;7use Illuminate\Support\Facades\Cache;8use Tests\TestCase;910class UserControllerTest extends TestCase11{12 public function testGetIndex()13 {14 Cache::shouldReceive('get')15 ->once()16 ->with('key')17 ->andReturn('value');1819 $response = $this->get('/users');2021 // ...22 }23}
1<?php23namespace Tests\Feature;45use Illuminate\Foundation\Testing\RefreshDatabase;6use Illuminate\Foundation\Testing\WithoutMiddleware;7use Illuminate\Support\Facades\Cache;8use Tests\TestCase;910class UserControllerTest extends TestCase11{12 public function testGetIndex()13 {14 Cache::shouldReceive('get')15 ->once()16 ->with('key')17 ->andReturn('value');1819 $response = $this->get('/users');2021 // ...22 }23}
You should not mock the Request
facade. Instead, pass the input you desire into the HTTP testing methods such as get
and post
when running your test. Likewise, instead of mocking the Config
facade, call the Config::set
method in your tests.
Facade Spies
If you would like to spy on a facade, you may call the spy
method on the corresponding facade. Spies are similar to mocks; however, spies record any interaction between the spy and the code being tested, allowing you to make assertions after the code is executed:
1use Illuminate\Support\Facades\Cache;23public function test_values_are_be_stored_in_cache()4{5 Cache::spy();67 $response = $this->get('/');89 $response->assertStatus(200);1011 Cache::shouldHaveReceived('put')->once()->with('name', 'Taylor', 10);12}
1use Illuminate\Support\Facades\Cache;23public function test_values_are_be_stored_in_cache()4{5 Cache::spy();67 $response = $this->get('/');89 $response->assertStatus(200);1011 Cache::shouldHaveReceived('put')->once()->with('name', 'Taylor', 10);12}
Bus Fake
When testing code that dispatches jobs, you typically want to assert that a given job was dispatched but not actually queue or execute the job. This is because the job's execution can normally be tested in a separate test class.
You may use the Bus
facade's fake
method to prevent jobs from being dispatched to the queue. Then, after executing the code under test, you may inspect which jobs the application attempted to dispatch using the assertDispatched
and assertNotDispatched
methods:
1<?php23namespace Tests\Feature;45use App\Jobs\ShipOrder;6use Illuminate\Foundation\Testing\RefreshDatabase;7use Illuminate\Foundation\Testing\WithoutMiddleware;8use Illuminate\Support\Facades\Bus;9use Tests\TestCase;1011class ExampleTest extends TestCase12{13 public function test_orders_can_be_shipped()14 {15 Bus::fake();1617 // Perform order shipping...1819 // Assert that a job was dispatched...20 Bus::assertDispatched(ShipOrder::class);2122 // Assert a job was not dispatched...23 Bus::assertNotDispatched(AnotherJob::class);2425 // Assert that a job was dispatched synchronously...26 Bus::assertDispatchedSync(AnotherJob::class);2728 // Assert that a job was not dipatched synchronously...29 Bus::assertNotDispatchedSync(AnotherJob::class);3031 // Assert that a job was dispatched after the response was sent...32 Bus::assertDispatchedAfterResponse(AnotherJob::class);3334 // Assert a job was not dispatched after response was sent...35 Bus::assertNotDispatchedAfterResponse(AnotherJob::class);3637 // Assert no jobs were dispatched...38 Bus::assertNothingDispatched();39 }40}
1<?php23namespace Tests\Feature;45use App\Jobs\ShipOrder;6use Illuminate\Foundation\Testing\RefreshDatabase;7use Illuminate\Foundation\Testing\WithoutMiddleware;8use Illuminate\Support\Facades\Bus;9use Tests\TestCase;1011class ExampleTest extends TestCase12{13 public function test_orders_can_be_shipped()14 {15 Bus::fake();1617 // Perform order shipping...1819 // Assert that a job was dispatched...20 Bus::assertDispatched(ShipOrder::class);2122 // Assert a job was not dispatched...23 Bus::assertNotDispatched(AnotherJob::class);2425 // Assert that a job was dispatched synchronously...26 Bus::assertDispatchedSync(AnotherJob::class);2728 // Assert that a job was not dipatched synchronously...29 Bus::assertNotDispatchedSync(AnotherJob::class);3031 // Assert that a job was dispatched after the response was sent...32 Bus::assertDispatchedAfterResponse(AnotherJob::class);3334 // Assert a job was not dispatched after response was sent...35 Bus::assertNotDispatchedAfterResponse(AnotherJob::class);3637 // Assert no jobs were dispatched...38 Bus::assertNothingDispatched();39 }40}
You may pass a closure to the available methods in order to assert that a job was dispatched that passes a given "truth test". If at least one job was dispatched that passes the given truth test then the assertion will be successful. For example, you may wish to assert that a job was dispatched for a specific order:
1Bus::assertDispatched(function (ShipOrder $job) use ($order) {2 return $job->order->id === $order->id;3});
1Bus::assertDispatched(function (ShipOrder $job) use ($order) {2 return $job->order->id === $order->id;3});
Job Chains
The Bus
facade's assertChained
method may be used to assert that a chain of jobs was dispatched. The assertChained
method accepts an array of chained jobs as its first argument:
1use App\Jobs\RecordShipment;2use App\Jobs\ShipOrder;3use App\Jobs\UpdateInventory;4use Illuminate\Support\Facades\Bus;56Bus::assertChained([7 ShipOrder::class,8 RecordShipment::class,9 UpdateInventory::class10]);
1use App\Jobs\RecordShipment;2use App\Jobs\ShipOrder;3use App\Jobs\UpdateInventory;4use Illuminate\Support\Facades\Bus;56Bus::assertChained([7 ShipOrder::class,8 RecordShipment::class,9 UpdateInventory::class10]);
As you can see in the example above, the array of chained jobs may be an array of the job's class names. However, you may also provide an array of actual job instances. When doing so, Laravel will ensure that the job instances are of the same class and have the same property values of the chained jobs dispatched by your application:
1Bus::assertChained([2 new ShipOrder,3 new RecordShipment,4 new UpdateInventory,5]);
1Bus::assertChained([2 new ShipOrder,3 new RecordShipment,4 new UpdateInventory,5]);
Job Batches
The Bus
facade's assertBatched
method may be used to assert that a batch of jobs was dispatched. The closure given to the assertBatched
method receives an instance of Illuminate\Bus\PendingBatch
, which may be used to inspect the jobs within the batch:
1use Illuminate\Bus\PendingBatch;2use Illuminate\Support\Facades\Bus;34Bus::assertBatched(function (PendingBatch $batch) {5 return $batch->name == 'import-csv' &&6 $batch->jobs->count() === 10;7});
1use Illuminate\Bus\PendingBatch;2use Illuminate\Support\Facades\Bus;34Bus::assertBatched(function (PendingBatch $batch) {5 return $batch->name == 'import-csv' &&6 $batch->jobs->count() === 10;7});
Event Fake
When testing code that dispatches events, you may wish to instruct Laravel to not actually execute the event's listeners. Using the Event
facade's fake
method, you may prevent listeners from executing, execute the code under test, and then assert which events were dispatched by your application using the assertDispatched
, assertNotDispatched
, and assertNothingDispatched
methods:
1<?php23namespace Tests\Feature;45use App\Events\OrderFailedToShip;6use App\Events\OrderShipped;7use Illuminate\Foundation\Testing\RefreshDatabase;8use Illuminate\Foundation\Testing\WithoutMiddleware;9use Illuminate\Support\Facades\Event;10use Tests\TestCase;1112class ExampleTest extends TestCase13{14 /**15 * Test order shipping.16 */17 public function test_orders_can_be_shipped()18 {19 Event::fake();2021 // Perform order shipping...2223 // Assert that an event was dispatched...24 Event::assertDispatched(OrderShipped::class);2526 // Assert an event was dispatched twice...27 Event::assertDispatched(OrderShipped::class, 2);2829 // Assert an event was not dispatched...30 Event::assertNotDispatched(OrderFailedToShip::class);3132 // Assert that no events were dispatched...33 Event::assertNothingDispatched();34 }35}
1<?php23namespace Tests\Feature;45use App\Events\OrderFailedToShip;6use App\Events\OrderShipped;7use Illuminate\Foundation\Testing\RefreshDatabase;8use Illuminate\Foundation\Testing\WithoutMiddleware;9use Illuminate\Support\Facades\Event;10use Tests\TestCase;1112class ExampleTest extends TestCase13{14 /**15 * Test order shipping.16 */17 public function test_orders_can_be_shipped()18 {19 Event::fake();2021 // Perform order shipping...2223 // Assert that an event was dispatched...24 Event::assertDispatched(OrderShipped::class);2526 // Assert an event was dispatched twice...27 Event::assertDispatched(OrderShipped::class, 2);2829 // Assert an event was not dispatched...30 Event::assertNotDispatched(OrderFailedToShip::class);3132 // Assert that no events were dispatched...33 Event::assertNothingDispatched();34 }35}
You may pass a closure to the assertDispatched
or assertNotDispatched
methods in order to assert that an event was dispatched that passes a given "truth test". If at least one event was dispatched that passes the given truth test then the assertion will be successful:
1Event::assertDispatched(function (OrderShipped $event) use ($order) {2 return $event->order->id === $order->id;3});
1Event::assertDispatched(function (OrderShipped $event) use ($order) {2 return $event->order->id === $order->id;3});
If you would simply like to assert that an event listener is listening to a given event, you may use the assertListening
method:
1Event::assertListening(2 OrderShipped::class,3 SendShipmentNotification::class4);
1Event::assertListening(2 OrderShipped::class,3 SendShipmentNotification::class4);
After calling Event::fake()
, no event listeners will be executed. So, if your tests use model factories that rely on events, such as creating a UUID during a model's creating
event, you should call Event::fake()
after using your factories.
Faking A Subset Of Events
If you only want to fake event listeners for a specific set of events, you may pass them to the fake
or fakeFor
method:
1/**2 * Test order process.3 */4public function test_orders_can_be_processed()5{6 Event::fake([7 OrderCreated::class,8 ]);910 $order = Order::factory()->create();1112 Event::assertDispatched(OrderCreated::class);1314 // Other events are dispatched as normal...15 $order->update([...]);16}
1/**2 * Test order process.3 */4public function test_orders_can_be_processed()5{6 Event::fake([7 OrderCreated::class,8 ]);910 $order = Order::factory()->create();1112 Event::assertDispatched(OrderCreated::class);1314 // Other events are dispatched as normal...15 $order->update([...]);16}
Scoped Event Fakes
If you only want to fake event listeners for a portion of your test, you may use the fakeFor
method:
1<?php23namespace Tests\Feature;45use App\Events\OrderCreated;6use App\Models\Order;7use Illuminate\Foundation\Testing\RefreshDatabase;8use Illuminate\Support\Facades\Event;9use Illuminate\Foundation\Testing\WithoutMiddleware;10use Tests\TestCase;1112class ExampleTest extends TestCase13{14 /**15 * Test order process.16 */17 public function test_orders_can_be_processed()18 {19 $order = Event::fakeFor(function () {20 $order = Order::factory()->create();2122 Event::assertDispatched(OrderCreated::class);2324 return $order;25 });2627 // Events are dispatched as normal and observers will run ...28 $order->update([...]);29 }30}
1<?php23namespace Tests\Feature;45use App\Events\OrderCreated;6use App\Models\Order;7use Illuminate\Foundation\Testing\RefreshDatabase;8use Illuminate\Support\Facades\Event;9use Illuminate\Foundation\Testing\WithoutMiddleware;10use Tests\TestCase;1112class ExampleTest extends TestCase13{14 /**15 * Test order process.16 */17 public function test_orders_can_be_processed()18 {19 $order = Event::fakeFor(function () {20 $order = Order::factory()->create();2122 Event::assertDispatched(OrderCreated::class);2324 return $order;25 });2627 // Events are dispatched as normal and observers will run ...28 $order->update([...]);29 }30}
HTTP Fake
The Http
facade's fake
method allows you to instruct the HTTP client to return stubbed / dummy responses when requests are made. For more information on faking outgoing HTTP requests, please consult the HTTP Client testing documentation.
Mail Fake
You may use the Mail
facade's fake
method to prevent mail from being sent. Typically, sending mail is unrelated to the code you are actually testing. Most likely, it is sufficient to simply assert that Laravel was instructed to send a given mailable.
After calling the Mail
facade's fake
method, you may then assert that mailables were instructed to be sent to users and even inspect the data the mailables received:
1<?php23namespace Tests\Feature;45use App\Mail\OrderShipped;6use Illuminate\Foundation\Testing\RefreshDatabase;7use Illuminate\Foundation\Testing\WithoutMiddleware;8use Illuminate\Support\Facades\Mail;9use Tests\TestCase;1011class ExampleTest extends TestCase12{13 public function test_orders_can_be_shipped()14 {15 Mail::fake();1617 // Perform order shipping...1819 // Assert that no mailables were sent...20 Mail::assertNothingSent();2122 // Assert that a mailable was sent...23 Mail::assertSent(OrderShipped::class);2425 // Assert a mailable was sent twice...26 Mail::assertSent(OrderShipped::class, 2);2728 // Assert a mailable was not sent...29 Mail::assertNotSent(AnotherMailable::class);30 }31}
1<?php23namespace Tests\Feature;45use App\Mail\OrderShipped;6use Illuminate\Foundation\Testing\RefreshDatabase;7use Illuminate\Foundation\Testing\WithoutMiddleware;8use Illuminate\Support\Facades\Mail;9use Tests\TestCase;1011class ExampleTest extends TestCase12{13 public function test_orders_can_be_shipped()14 {15 Mail::fake();1617 // Perform order shipping...1819 // Assert that no mailables were sent...20 Mail::assertNothingSent();2122 // Assert that a mailable was sent...23 Mail::assertSent(OrderShipped::class);2425 // Assert a mailable was sent twice...26 Mail::assertSent(OrderShipped::class, 2);2728 // Assert a mailable was not sent...29 Mail::assertNotSent(AnotherMailable::class);30 }31}
If you are queueing mailables for delivery in the background, you should use the assertQueued
method instead of assertSent
:
1Mail::assertQueued(OrderShipped::class);23Mail::assertNotQueued(OrderShipped::class);45Mail::assertNothingQueued();
1Mail::assertQueued(OrderShipped::class);23Mail::assertNotQueued(OrderShipped::class);45Mail::assertNothingQueued();
You may pass a closure to the assertSent
, assertNotSent
, assertQueued
, or assertNotQueued
methods in order to assert that a mailable was sent that passes a given "truth test". If at least one mailable was sent that passes the given truth test then the assertion will be successful:
1Mail::assertSent(function (OrderShipped $mail) use ($order) {2 return $mail->order->id === $order->id;3});
1Mail::assertSent(function (OrderShipped $mail) use ($order) {2 return $mail->order->id === $order->id;3});
When calling the Mail
facade's assertion methods, the mailable instance accepted by the provided closure exposes helpful methods for examining the recipients of the mailable:
1Mail::assertSent(OrderShipped::class, function ($mail) use ($user) {2 return $mail->hasTo($user->email) &&3 $mail->hasCc('...') &&4 $mail->hasBcc('...');5});
1Mail::assertSent(OrderShipped::class, function ($mail) use ($user) {2 return $mail->hasTo($user->email) &&3 $mail->hasCc('...') &&4 $mail->hasBcc('...');5});
You may have noticed that there are two methods for asserting that mail was not sent: assertNotSent
and assertNotQueued
. Sometimes you may wish to assert that no mail was sent or queued. To accomplish this, you may use the assertNothingOutgoing
and assertNotOutgoing
methods:
1Mail::assertNothingOutgoing();23Mail::assertNotOutgoing(function (OrderShipped $mail) use ($order) {4 return $mail->order->id === $order->id;5});
1Mail::assertNothingOutgoing();23Mail::assertNotOutgoing(function (OrderShipped $mail) use ($order) {4 return $mail->order->id === $order->id;5});
Notification Fake
You may use the Notification
facade's fake
method to prevent notifications from being sent. Typically, sending notifications is unrelated to the code you are actually testing. Most likely, it is sufficient to simply assert that Laravel was instructed to send a given notification.
After calling the Notification
facade's fake
method, you may then assert that notifications were instructed to be sent to users and even inspect the data the notifications received:
1<?php23namespace Tests\Feature;45use App\Notifications\OrderShipped;6use Illuminate\Foundation\Testing\RefreshDatabase;7use Illuminate\Foundation\Testing\WithoutMiddleware;8use Illuminate\Support\Facades\Notification;9use Tests\TestCase;1011class ExampleTest extends TestCase12{13 public function test_orders_can_be_shipped()14 {15 Notification::fake();1617 // Perform order shipping...1819 // Assert that no notifications were sent...20 Notification::assertNothingSent();2122 // Assert a notification was sent to the given users...23 Notification::assertSentTo(24 [$user], OrderShipped::class25 );2627 // Assert a notification was not sent...28 Notification::assertNotSentTo(29 [$user], AnotherNotification::class30 );31 }32}
1<?php23namespace Tests\Feature;45use App\Notifications\OrderShipped;6use Illuminate\Foundation\Testing\RefreshDatabase;7use Illuminate\Foundation\Testing\WithoutMiddleware;8use Illuminate\Support\Facades\Notification;9use Tests\TestCase;1011class ExampleTest extends TestCase12{13 public function test_orders_can_be_shipped()14 {15 Notification::fake();1617 // Perform order shipping...1819 // Assert that no notifications were sent...20 Notification::assertNothingSent();2122 // Assert a notification was sent to the given users...23 Notification::assertSentTo(24 [$user], OrderShipped::class25 );2627 // Assert a notification was not sent...28 Notification::assertNotSentTo(29 [$user], AnotherNotification::class30 );31 }32}
You may pass a closure to the assertSentTo
or assertNotSentTo
methods in order to assert that a notification was sent that passes a given "truth test". If at least one notification was sent that passes the given truth test then the assertion will be successful:
1Notification::assertSentTo(2 $user,3 function (OrderShipped $notification, $channels) use ($order) {4 return $notification->order->id === $order->id;5 }6);
1Notification::assertSentTo(2 $user,3 function (OrderShipped $notification, $channels) use ($order) {4 return $notification->order->id === $order->id;5 }6);
On-Demand Notifications
If the code you are testing sends on-demand notifications, you will need to assert that the notification was sent to an Illuminate\Notifications\AnonymousNotifiable
instance:
1use Illuminate\Notifications\AnonymousNotifiable;23Notification::assertSentTo(4 new AnonymousNotifiable, OrderShipped::class5);
1use Illuminate\Notifications\AnonymousNotifiable;23Notification::assertSentTo(4 new AnonymousNotifiable, OrderShipped::class5);
By passing a closure as the third argument to the notification assertion methods, you may determine if an on-demand notification was sent to the correct "route" address:
1Notification::assertSentTo(2 new AnonymousNotifiable,3 OrderShipped::class,4 function ($notification, $channels, $notifiable) use ($user) {5 return $notifiable->routes['mail'] === $user->email;6 }7);
1Notification::assertSentTo(2 new AnonymousNotifiable,3 OrderShipped::class,4 function ($notification, $channels, $notifiable) use ($user) {5 return $notifiable->routes['mail'] === $user->email;6 }7);
Queue Fake
You may use the Queue
facade's fake
method to prevent queued jobs from being pushed to the queue. Most likely, it is sufficient to simply assert that Laravel was instructed to push a given job to the queue since the queued jobs themselves may be tested in another test class.
After calling the Queue
facade's fake
method, you may then assert that the application attempted to push jobs to the queue:
1<?php23namespace Tests\Feature;45use App\Jobs\AnotherJob;6use App\Jobs\FinalJob;7use App\Jobs\ShipOrder;8use Illuminate\Foundation\Testing\RefreshDatabase;9use Illuminate\Foundation\Testing\WithoutMiddleware;10use Illuminate\Support\Facades\Queue;11use Tests\TestCase;1213class ExampleTest extends TestCase14{15 public function test_orders_can_be_shipped()16 {17 Queue::fake();1819 // Perform order shipping...2021 // Assert that no jobs were pushed...22 Queue::assertNothingPushed();2324 // Assert a job was pushed to a given queue...25 Queue::assertPushedOn('queue-name', ShipOrder::class);2627 // Assert a job was pushed twice...28 Queue::assertPushed(ShipOrder::class, 2);2930 // Assert a job was not pushed...31 Queue::assertNotPushed(AnotherJob::class);32 }33}
1<?php23namespace Tests\Feature;45use App\Jobs\AnotherJob;6use App\Jobs\FinalJob;7use App\Jobs\ShipOrder;8use Illuminate\Foundation\Testing\RefreshDatabase;9use Illuminate\Foundation\Testing\WithoutMiddleware;10use Illuminate\Support\Facades\Queue;11use Tests\TestCase;1213class ExampleTest extends TestCase14{15 public function test_orders_can_be_shipped()16 {17 Queue::fake();1819 // Perform order shipping...2021 // Assert that no jobs were pushed...22 Queue::assertNothingPushed();2324 // Assert a job was pushed to a given queue...25 Queue::assertPushedOn('queue-name', ShipOrder::class);2627 // Assert a job was pushed twice...28 Queue::assertPushed(ShipOrder::class, 2);2930 // Assert a job was not pushed...31 Queue::assertNotPushed(AnotherJob::class);32 }33}
You may pass a closure to the assertPushed
or assertNotPushed
methods in order to assert that a job was pushed that passes a given "truth test". If at least one job was pushed that passes the given truth test then the assertion will be successful:
1Queue::assertPushed(function (ShipOrder $job) use ($order) {2 return $job->order->id === $order->id;3});
1Queue::assertPushed(function (ShipOrder $job) use ($order) {2 return $job->order->id === $order->id;3});
Job Chains
The Queue
facade's assertPushedWithChain
and assertPushedWithoutChain
methods may be used to inspect the job chain of a pushed job. The assertPushedWithChain
method accepts the primary job as its first argument and an array of chained jobs as its second argument:
1use App\Jobs\RecordShipment;2use App\Jobs\ShipOrder;3use App\Jobs\UpdateInventory;4use Illuminate\Support\Facades\Queue;56Queue::assertPushedWithChain(ShipOrder::class, [7 RecordShipment::class,8 UpdateInventory::class9]);
1use App\Jobs\RecordShipment;2use App\Jobs\ShipOrder;3use App\Jobs\UpdateInventory;4use Illuminate\Support\Facades\Queue;56Queue::assertPushedWithChain(ShipOrder::class, [7 RecordShipment::class,8 UpdateInventory::class9]);
As you can see in the example above, the array of chained jobs may be an array of the job's class names. However, you may also provide an array of actual job instances. When doing so, Laravel will ensure that the job instances are of the same class and have the same property values of the chained jobs dispatched by your application:
1Queue::assertPushedWithChain(ShipOrder::class, [2 new RecordShipment,3 new UpdateInventory,4]);
1Queue::assertPushedWithChain(ShipOrder::class, [2 new RecordShipment,3 new UpdateInventory,4]);
You may use the assertPushedWithoutChain
method to assert that a job was pushed without a chain of jobs:
1Queue::assertPushedWithoutChain(ShipOrder::class);
1Queue::assertPushedWithoutChain(ShipOrder::class);
Storage Fake
The Storage
facade's fake
method allows you to easily generate a fake disk that, combined with the file generation utilities of the Illuminate\Http\UploadedFile
class, greatly simplifies the testing of file uploads. For example:
1<?php23namespace Tests\Feature;45use Illuminate\Foundation\Testing\RefreshDatabase;6use Illuminate\Foundation\Testing\WithoutMiddleware;7use Illuminate\Http\UploadedFile;8use Illuminate\Support\Facades\Storage;9use Tests\TestCase;1011class ExampleTest extends TestCase12{13 public function test_albums_can_be_uploaded()14 {15 Storage::fake('photos');1617 $response = $this->json('POST', '/photos', [18 UploadedFile::fake()->image('photo1.jpg'),19 UploadedFile::fake()->image('photo2.jpg')20 ]);2122 // Assert one or more files were stored...23 Storage::disk('photos')->assertExists('photo1.jpg');24 Storage::disk('photos')->assertExists(['photo1.jpg', 'photo2.jpg']);2526 // Assert one or more files were not stored...27 Storage::disk('photos')->assertMissing('missing.jpg');28 Storage::disk('photos')->assertMissing(['missing.jpg', 'non-existing.jpg']);29 }30}
1<?php23namespace Tests\Feature;45use Illuminate\Foundation\Testing\RefreshDatabase;6use Illuminate\Foundation\Testing\WithoutMiddleware;7use Illuminate\Http\UploadedFile;8use Illuminate\Support\Facades\Storage;9use Tests\TestCase;1011class ExampleTest extends TestCase12{13 public function test_albums_can_be_uploaded()14 {15 Storage::fake('photos');1617 $response = $this->json('POST', '/photos', [18 UploadedFile::fake()->image('photo1.jpg'),19 UploadedFile::fake()->image('photo2.jpg')20 ]);2122 // Assert one or more files were stored...23 Storage::disk('photos')->assertExists('photo1.jpg');24 Storage::disk('photos')->assertExists(['photo1.jpg', 'photo2.jpg']);2526 // Assert one or more files were not stored...27 Storage::disk('photos')->assertMissing('missing.jpg');28 Storage::disk('photos')->assertMissing(['missing.jpg', 'non-existing.jpg']);29 }30}
For more information on testing file uploads, you may consult the HTTP testing documentation's information on file uploads.
By default, the fake
method will delete all files in its temporary directory. If you would like to keep these files, you may use the "persistentFake" method instead.
Interacting With Time
When testing, you may occasionally need to modify the time returned by helpers such as now
or Illuminate\Support\Carbon::now()
. Thankfully, Laravel's base feature test class includes helpers that allow you to manipulate the current time:
1public function testTimeCanBeManipulated()2{3 // Travel into the future...4 $this->travel(5)->milliseconds();5 $this->travel(5)->seconds();6 $this->travel(5)->minutes();7 $this->travel(5)->hours();8 $this->travel(5)->days();9 $this->travel(5)->weeks();10 $this->travel(5)->years();1112 // Travel into the past...13 $this->travel(-5)->hours();1415 // Travel to an explicit time...16 $this->travelTo(now()->subHours(6));1718 // Return back to the present time...19 $this->travelBack();20}
1public function testTimeCanBeManipulated()2{3 // Travel into the future...4 $this->travel(5)->milliseconds();5 $this->travel(5)->seconds();6 $this->travel(5)->minutes();7 $this->travel(5)->hours();8 $this->travel(5)->days();9 $this->travel(5)->weeks();10 $this->travel(5)->years();1112 // Travel into the past...13 $this->travel(-5)->hours();1415 // Travel to an explicit time...16 $this->travelTo(now()->subHours(6));1718 // Return back to the present time...19 $this->travelBack();20}