You don't need to use fakes straight away
In Laravel test suites it's common to set up all your fakes straight away. It's good practice to clearly set up some expectation about a test. Enabling a fake (queues, notification, or events) can signal that the test involves making assertions on those fakes.
Alternatively it can be used to simply stub out some behaviour we don't want to engage. For example, disabling events because they aren't relevant in a given scenario.
Sometimes the test setup may invoke different behaviour on the same facade. In a test scenario you may want some behaviour to run but stub out others to make assertions on. Most of Laravel's test facades allow you to do this explicitly by passing in what you want to stub out.
However there are use-cases where you may not want to enable a fake straight away. If your models have observers that will trigger behaviour on creation that you also want to test inside a request, moving the fake after the observers have fired will let you be specific about your assertion.
There are other ways to achieve this, but I'm not as keen on them:
- You could keep the queue fake at the beginning and get the test passing by increating the count given to the assertion - like
Queue::assertPushedTimes(ReindexProfile::class, 2)
. However that isn't as clear as simply moving the fake around and can be more brittle. - You could create the model with
Profile::factory()->createQuietly()
if you don't actually need the behaviour to run. But again the intention isn't as clear.