In this tutorial, we aim to understand the concepts of mocking and faking in tests. Mocking and faking are fundamental concepts in software testing that allow us to isolate the system under test and create a controlled environment.
By the end of this tutorial, you will learn:
- What is mocking and faking
- How to create mock data for your tests
- How to use Laravel's faking features
Prerequisites:
- Basic understanding of PHP and Laravel
- Basic understanding of unit testing
Mocking is creating objects that simulate the behavior of real objects. We use them when we don't want to invoke production code or when the real object is difficult to incorporate into the unit test.
In Laravel, we can use a package called Mockery to create our mock objects. Here's an example:
$mock = Mockery::mock('ClassToMock');
$mock->shouldReceive('methodToMock')
->once()
->andReturn('mocked value');
Faking is a little bit different. Rather than creating a mock object and defining the behavior of all its methods, we use a real object with the same interface that has been simplified for testing.
Laravel provides several helpers to generate fakes. For example, to fake notifications, we can do:
Notification::fake();
// Perform order dispatch...
Notification::assertSentTo(
$user,
OrderShipped::class,
function ($notification, $channels) use ($order) {
return $notification->order->id === $order->id;
}
);
Let's see some practical examples.
// Create a mock object for the User class
$userMock = Mockery::mock('App\User');
// Define the behavior for the getName method
$userMock->shouldReceive('getName')
->once()
->andReturn('John Doe');
// Use the mock object
echo $userMock->getName(); // Outputs: John Doe
In the example above, we create a mock object for the User class and define that the getName method should return 'John Doe' when it's called.
// Fake the Notification facade
Notification::fake();
$order = factory(Order::class)->create();
// Dispatch an order
dispatch(new ShipOrder($order));
// Assert a notification was sent to the given users...
Notification::assertSentTo(
$order->user,
OrderShipped::class,
function ($notification, $channels) use ($order) {
// Check that the order that was shipped is the correct one
return $notification->order->id === $order->id;
}
);
In this example, we fake the Notification facade, dispatch an order, and then assert that a notification was sent.
Happy coding!