Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bwrigley's avatar

Correct way to assert that mock expectations have passed.

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);
        }
    }
0 likes
8 replies
Sti3bas's avatar

@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();
}
Sti3bas's avatar
Sti3bas
Best Answer
Level 53

@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)
bwrigley's avatar

@sti3bas we are still on 5.8.31, but I would have expected it to still work

<?php

namespace Tests\Feature\Reports;

use AnalyticsMetric;
use App\Analytics\Contracts\AnalyticsClient;
use App\Analytics\Reports\PageViewReport;
use App\Analytics\Services\TestAnalyticsClient;
use Mockery;
use Tests\TestCase;

class PageViewReportTest extends TestCase
{
    /**
     * @test
     * @group report
     */
    public function test_page_view_report(): void
    {
        $analyticsClient = Mockery::spy(TestAnalyticsClient::class)->makePartial();
        $this->app->instance(AnalyticsClient::class, $analyticsClient);

        $report = new PageViewReport;
        $report->generate();

        $analyticsClient->shouldHaveReceived('withMetrics')->with([AnalyticsMetric::PAGE_VIEWS])->once();

    }

}

PHPUnit 7.5.2 by Sebastian Bergmann and contributors.

R                                                                   1 / 1 (100%)

Time: 1.71 seconds, Memory: 24.00MB

There was 1 risky test:

1) Tests\Feature\Reports\PageViewReportTest::test_page_view_report
This test did not perform any assertions

/home/vagrant/Code/everyone-in-mind/tests/Feature/Reports/PageViewReportTest.php:22

OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Risky: 1.
Sti3bas's avatar

@bwrigley I can confirm that my example works in Laravel 5.8.31 and PHPUnit 7.5.2. Do you made any modifications to TestCase class?

bwrigley's avatar

@sti3bas wow that's strange. No, no modifications. Thank you so much for testing though.

bwrigley's avatar

@sti3bas It wasn't hitting for some reason.

I wondered if it's the fact I'm running in the Homestead environment, so I have halted, reprovisioned, and restarted and it's working now!

Thank you for helping me get there

1 like

Please or to participate in this conversation.