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

- URL: https://laravel-vuejs.space/pest-fake-ai-agent-vue
- Author: Anass Ez-zouaine (https://laravel-vuejs.space/author/anass-ez-zouaine)
- Published: 2026-10-11
- Topics: Laravel, VueJs, Testing

People searching “Pest fake AI agent” hit the real model from a feature test. Fake the agent. The [tests guide](https://laravel-vuejs.space/test-laravel-vue) is the rest of the feature test. The [AI SDK guide](https://laravel-vuejs.space/laravel-ai-sdk-vue) 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

**Q: 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.

**Q: 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.
