Previously, I shared how adding Http::assertSentCount(1) to your tests can catch a sneaky bug where the same request gets fired twice.
When Aaron was reviewing that tip, he asked a great follow-up question.
Could Http::sequence solve the same problem in a different way?
If you've never used it before, a sequence lets you queue up one or more fake responses, and each outgoing request consumes the next response in the queue.
You can pass Http::sequence() as the value for a URL pattern inside Http::fake, or use the top-level Http::fakeSequence shortcut when you only need a single sequence.
Under the hood, they both use the same mechanism.
Normally, I'd use this when I cared about the return values for consecutive Http calls, but to Aaron's point, there's an added benefit related to accidental duplicates.
As he hinted at with his question, if you specify only one response in the sequence, and then the code under test makes two calls, the second call will throw an OutOfBoundsException with the message "A request was made, but the response sequence is empty."
So this approach would catch the same duplicate-request bug, but without any extra count assertion.
There is a trade-off to be aware of, though.
With assertSentCount(1), a failing test gives you a clean count mismatch message.
With the sequence approach, you get an exception mid-execution, which can sometimes obscure the real assertion you cared about.
But if you're already setting up sequences for other reasons, you get the count check for free.
Here to help,
Joel
P.S. Want to level up your Laravel testing skills? Our workshop covers patterns like this and a whole lot more. Check out our testing workshop.