People searching “Pest fake AI agent” hit the real model from a feature test. Fake the agent. The tests guide is the rest of the feature test. The AI SDK guide is the controller this test hits.
TL;DR
- DraftReply::fake() returns a canned string.
- assertPrompted checks the note.
- The test does not call the provider.
- Do not assert on the provider’s JSON.
The fix#
use App\Ai\Agents\DraftReply;
use App\Models\User;
it('drafts a reply', function () {
DraftReply::fake(['Rewritten.']);
$user = User::factory()->create();
$this->actingAs($user)
->post('/replies', ['note' => 'Ship the form.'])
->assertRedirect()
->assertSessionHas('draft', 'Rewritten.');
DraftReply::assertPrompted('Ship the form.');
});fake prevents the HTTP call. assertPrompted fails when the controller sent $request->all() or an empty string. preventStrayPrompts() fails the test when some other agent ran.
What to avoid#
Gotcha
Do not assert on the provider’s JSON. You faked past it. Do not leave a real key in .env.testing and skip the fake “just for one test”. That test will cost money and flap.
FAQ#
How do I fake an AI agent in a Pest test?
DraftReply::fake() returns a canned string. assertPrompted checks the note. The test does not call the provider.
What goes wrong with Pest fake AI agent?
Many developers hit the real model from a feature test. Do not assert on the provider's JSON.



