orchestral/testbench

[7.x] `expectsEvents()` changed/deprecated?

cviebrock opened this issue · 3 comments

  • Testbench Version: 7.x-dev
  • Laravel Version: ^9.0
  • PHP Version: 8.1.1
  • Database Driver & Version: MySQL 8.0.27

Description:

I'm upgrading my cviebrock/eloquent-taggable package for Laravel 9 support, and running into an error with this code from one of my test suites:

    public function testModelTaggedEvent(): void
    {
        $this->expectsEvents(ModelTagged::class);

        $this->testModel->tag('Apple');
    }

The error:

Error: Call to undefined method Cviebrock\EloquentTaggable\Test\EventTests::expectsEvents()

Has the syntax for testing for events changed in the new version?

Steps To Reproduce:

Check out the last run of GitHub actions here: https://github.com/cviebrock/eloquent-taggable/actions/runs/1743404354

Or, to test locally:

  • check out the package: git clone https://github.com/cviebrock/eloquent-taggable.git
  • composer install
  • make sure there is a MySQL server running on localhost (user root, db testing)
  • composer run tests

None that I'm aware of.

On further investigation, it was due to the removal of the deprecated MocksApplicationServices trait from the default TestCase. laravel/framework#36716

You may still use it for now by adding it back to your base TestCase class or move to the latest facade mocking options.

p/s: will add a note on this on the changelog.

I stumbled upon this as well.
Anyone else looking for an up-to-date way of doing it https://laravel.com/docs/master/mocking#event-fake needs to be used.

Example:

Old code:

$this->expectsEvents([PaymentDeclined::class]);
// test code that fires the events

New code:

Event::fake();
// test code that fires events
Event::assertDispatched(PaymentDeclined::class);