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.

For your AI agent

Copies a prompt. Paste it into the AI agent you already have open.

Read https://laravel-vuejs.space/pest-fake-ai-agent-vue.md
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.
Do not leave a real key in .env.testing and skip the fake "just for one test".
Show the diff. Do not commit.
On this page · 3 sections
  1. The fix
  2. What to avoid
  3. FAQ

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.

Who it's for: Laravel + Vue developers

Applies to: Laravel 13 · Vue 3

The fix#

php
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.

Found this useful?

Ask questions, share what you built.

Join thousands of Laravel and Vue developers in our Facebook group, or get new posts in your Telegram feed. Sharing this article helps too.

Keep reading

Newsletter

Get the best of Laravel & Vue, once a month.

One short email with new guides, release notes worth reading and hand-picked community links. No spam, unsubscribe any time.