Testing
The package provides dedicated testing fakes for all four facades, allowing you to mock LLM responses and make assertions about how your code interacts with the services. No real API calls are made when a fake is active, which makes tests fast, deterministic, and free of external dependencies.StructuredOutput::fake()
TheStructuredOutputFake intercepts all extraction calls and returns predefined responses. It records every call so you can assert against the response model class, messages, connection, and model that were used.
Basic Usage
Response Mapping
Map response model classes to their fake responses. Each class returns its corresponding value when extracted.RuntimeException with a helpful message telling you which class needs a fake response.
Response Sequences
Return different responses for sequential calls to the same response model class.Available Assertions
Accessing Recorded Calls
Inspect all recorded extraction calls for custom assertions.Inference::fake()
TheInferenceFake intercepts raw inference calls and returns responses based on pattern matching against the input messages.
Basic Usage
Pattern Matching
Responses are matched by checking whether the input message contains the pattern string. The first matching pattern wins. If no pattern matches, thedefault key is used as a fallback; if no default exists, an empty string is returned.
Response Sequences
Queue ordered responses that are returned regardless of input content.Available Assertions
Embeddings::fake()
TheEmbeddingsFake intercepts embedding requests and returns predefined or randomly generated vectors.
Basic Usage
Default Embeddings
If no pattern matches, a random normalized embedding vector is generated automatically. This is useful when you need an embedding but do not care about its exact values.Custom Dimensions
Match the dimensionality of your production embedding model.Available Assertions
AgentCtrl::fake()
TheAgentCtrlFake intercepts code agent executions and returns predefined responses without launching any CLI processes.
Basic Usage
Response Sequences
Return different responses for sequential calls. If more calls are made than responses provided, the last response is repeated.Custom Responses
Create detailed fake responses with specific metadata using theAgentCtrlFake::response() factory method.
Fake Tool Calls
Simulate agent tool usage in your tests.Available Assertions
Testing Agent Services
HTTP Client Faking
Since the package routes all HTTP traffic through Laravel’s HTTP client (Illuminate\Http\Client\Factory), you can also use Http::fake() to intercept requests at the HTTP transport level. This approach is lower-level than facade fakes and is useful when you need to test specific HTTP request/response shapes.
LaravelDriver HTTP transport uses the same Illuminate\Http\Client\Factory instance that Http::fake() instruments. Make sure the instructor.http.driver config is set to 'laravel' (the default).
Testing Services
When testing services that use Instructor through dependency injection, the facade fake automatically replaces the container binding. The container will resolve the fake instance for both facade calls and injected dependencies.Best Practices
1. Always Setup Fakes First
Callfake() before any code that might trigger an extraction. Setting up a fake after the fact has no effect on calls that already happened.
2. Use Realistic Test Data
Realistic fake responses help catch bugs that only surface with production-like data, such as edge cases in string formatting or numeric precision.3. Test Edge Cases
Verify that your code handles empty collections, null optional fields, and other boundary conditions correctly.4. Verify Connection and Model Usage
Assert that your code routes requests to the correct provider and model, especially when different code paths use different connections.Native Agent Testing
ResolveCognesy\Instructor\Laravel\Testing\NativeAgentTesting from the container when you want native-agent runtime tests without app-local glue.
It can:
- register a
FakeAgentDrivercapability - swap session storage to in-memory mode
- replace broadcasting with a recording transport
- replace telemetry export with a recording exporter
packages/laravel immediately use the swapped testing surfaces.