Testing

Fake components to help you test your AI powered system

When you test an agent, you don't want every test run to make real API calls to OpenAI, Anthropic, or any other provider. Real calls are slow, cost money, and return different results every time, making your tests flaky and expensive. The same applies to RAG agents: you don't want to spin up a vector database or call an embeddings API just to verify your agent's logic.

Neuron ships with drop-in test doubles that solve this problem. FakeAIProvider replaces the AI provider, FakeEmbeddingsProvider replaces the embeddings provider, and FakeVectorStore replaces the vector store. They return predetermined responses, never hit the network, and record every interaction so you can assert exactly what your agent did.

Setup

Create a FakeAIProvider with the responses you expect the model to return, then inject it into your agent:

use NeuronAI\Chat\Messages\Stream\AssistantMessage;
use NeuronAI\Testing\FakeAIProvider;

$provider = new FakeAIProvider(
    new AssistantMessage('Hello! How can I help you?')
);

$agent = MyAgent::make()->setAiProvider($provider);

Responses are returned in order. If your agent makes multiple calls to the provider (e.g. tool calls), queue multiple responses:

$provider = new FakeAIProvider(
    new AssistantMessage('First response'),
    new AssistantMessage('Second response'),
);

Chat

public function test_agent_responds(): void
{
    $provider = new FakeAIProvider(
        new AssistantMessage('The capital of France is Paris.')
    );

    $agent = MyAgent::make()->setAiProvider($provider);

    $message = $agent->chat(new UserMessage('What is the capital of France?'))->getMessage();

    $this->assertSame('The capital of France is Paris.', $message->getContent());
    $provider->assertCallCount(1);
}

Streaming

The fake provider splits the response text into chunks, simulating a real stream:

You can change the chunk size with setStreamChunkSize():

Structured Output

Provide a JSON string that matches your output class schema. The agent will deserialize and validate it as usual:

Tool Calls

When the model decides to call a tool, it returns a ToolCallMessage. The agent executes the tool and loops back to the provider for a final answer. Queue both responses:

Assertions

FakeAIProvider includes built-in assertions you can use in your tests:

Inspecting Requests

For more advanced checks, access the raw recorded requests:

RAG

RAG agents depend on an embeddings provider and a vector store in addition to the AI provider. Neuron provides FakeEmbeddingsProvider and FakeVectorStore to replace both in tests.

FakeEmbeddingsProvider

Generates deterministic embeddings without calling any external API. Drop it in wherever you need an embeddings provider:

FakeVectorStore

Returns predetermined documents from similaritySearch() regardless of the embedding passed in. Pass the documents you want returned to the constructor:

RAG Chat

Adding Documents

Test that your indexing pipeline correctly embeds and stores documents:

RAG Assertions

Last updated