Laravel Mock with Mockery has error "no expectations were specified"
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Laravel Mock with Mockery: Resolving "no expectations were specified" Error
Introduction:
Mocking an object is a crucial test technique in software development that helps ensure that your code works as expected, even when interacting with different implementations of the same interface. In this article, we'll address a common issue encountered during Laravel Mock with Mockery – "no expectations were specified" error. We will guide you through understanding why this happens and how you can fix it by following best practices in writing unit tests.
Step 1: Understand the Basics
In order to write effective mocking code, you must first understand what mocks are and how they work with Laravel. A mock is a dummy object that helps you verify that another class's method was called during testing without modifying its original behavior. This enables you to test specific scenarios without worrying about side effects on the actual database or external systems.
Step 2: Set Up Your Test Case and Mocks
To start, create a new unit test file with a VisitorStatisticTest class. Within this class, initialize your mock for the VisitorStatisticRepository as follows:
public function testSetVisitorLog()
{
$visitorRepositoryMock = Mockery::mock(VisitorStatisticRepository::class);
// ... Your code for setting up your mock goes here...
}
Step 3: Testing Method Calls with Mocks
Now that you've initialized the mocks, you can use them to verify method calls during your test. In our example, we were testing whether or not the setLog() method on VisitorStatisticRepository was called when instantiating the VisitorStatisticAction class:
public function testSetVisitorLog()
{
$visitorRepositoryMock = Mockery::mock(VisitorStatisticRepository::class);
// ... Your code for setting up your mock goes here...
$visit = $visitorAction->setLog('gilan');
$this->assertInstanceOf(VisitorStatistic::class, $visit);
$mock = $this->app->instance(VisitorStatisticRepository::class,$visitorRepositoryMock);
$mock->shouldReceive('setLog')->once()->andReturn(true); // add this line to verify your method call
}
Now, when running the test, Mockery will check if the setLog() method was called with the given arguments at least once. If it was not called, an exception will be raised.
Step 4: Resolving the "No Expectations Specified" Error
The error "Received Mockery_0_App_Repositories_VisitorStatisticRepository::setLog(), but no expectations were specified" is thrown when you're trying to call a method on a mocked object without specifying any expectations. It indicates that your mocking code doesn't define the expected behavior of the mocks or verify their calls.
In order to fix this issue, you can follow these steps:
1. Carefully examine the tested class and determine which methods must be called during testing.
2. Add appropriate expectation lines within your test case to validate those method calls were made. You can use Mockery's shouldReceive() function with the desired method name and parameters as arguments.
3. Run the test again, and the error should no longer appear. Remember that if you miss-specify any method calls in your expectations, the tests will fail.
Conclusion:
By following these steps, you can set up effective Laravel Mock with Mockery to ensure your code behaves as expected during testing. By using mocks and specifying expectations for your mock objects, you prevent unexpected behavior that could lead to issues later in production. Always keep best practices in mind when working with unit tests, and feel free to leverage the power of Mockery to make your tests more efficient and thorough.