@bwrigley does your test class extends Laravel's TestCase?
If no, then you need to manually call Mockery::close(). Add this at the end of your test class:
public function tearDown() {
\Mockery::close();
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
When writing tests and using Mockery to spy on classes, is there a correct way to assert that an expectation has passed?
I have this solution at the moment which is obviously wrong, but is there a right way?
public function test_page_view_report(): void
{
$analyticsClient = Mockery::spy(TestAnalyticsClient::class)->makePartial();
$this->app->instance(AnalyticsClient::class, $analyticsClient);
$report = new PageViewReport;
$report->generate();
try{
$analyticsClient->shouldHaveReceived('withMetrics')->with([AnalyticsMetric::PAGE_VIEWS])->once();
$this->assertTrue(true);
} catch (Exception $e) {
$this->assertTrue(false);
}
}
@bwrigley it works fine with the fresh Laravel 6 installation. Might be something with your site. What Laravel version you're using? Can you show the full test class?
/** @test */
class ExampleTest extends TestCase
{
/** @test */
public function dummy_test()
{
$spy = Mockery::spy(DummyClass2::class);
$this->app->instance(DummyClass2::class, $spy);
(new DummyClass)->dummyMethod();
$spy->shouldHaveReceived('dummyMethod')->with('test')->once();
$spy->shouldHaveReceived('dummyMethod2')->with('test2')->once();
}
}
PHPUnit 8.3.5 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 210 ms, Memory: 16.00 MB
OK (1 test, 2 assertions)
Please or to participate in this conversation.