Use the fake with any code that takes Microsoft.Extensions.AI’s IChatClient.
dotnet add package RecursiveAI.OneLlm.Extensions.AIRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using Microsoft.Extensions.AI; using OneLlm.Extensions.AI; using OneLlm.Testing; var fake = new ScriptedProvider("test"); fake.EnqueueText("Ready"); using IChatClient chat = new OneLlmChatClient (fake, "test-model", TimeProvider.System); Console.WriteLine((await chat .GetResponseAsync("Status?")).Text);
Unzip the example, then dotnet run.
ReadyFull program
Pass the fake client to ReadStatusAsync just as you would a production client. The program checks both what the method returns and what prompt it sent. No API key or cloud account is needed. If the code makes more requests than you queued replies for, the fake throws InvalidOperationException instead of returning a default answer.
using Microsoft.Extensions.AI; using OneLlm.Extensions.AI; using OneLlm.Testing; var fake = new ScriptedProvider("test"); fake.EnqueueText(" READY "); using IChatClient chat = new OneLlmChatClient( fake, "test-model", TimeProvider.System); string status = await ReadStatusAsync(chat); if (status != "ready") throw new InvalidOperationException($"Expected ready, got {status}"); string sent = fake.Requests.Single().Messages.Single().GetUserText(); if (sent != "Status?") throw new InvalidOperationException($"Unexpected prompt: {sent}"); Console.WriteLine($"Status: {status}"); Console.WriteLine($"Prompt: {sent}"); static async Task<string> ReadStatusAsync(IChatClient chat) { var reply = await chat.GetResponseAsync("Status?"); return reply.Text.Trim().ToLowerInvariant(); }
Status: ready Prompt: Status?
Variations
Test a streaming consumer. With fake and chat from the snippet at the top, queue a fresh reply before starting the stream. This prints the text from the updates in order.
fake.EnqueueText("Streaming reply"); await foreach (var update in chat.GetStreamingResponseAsync("Status?")) { Console.Write(update.Text); } Console.WriteLine();
Streaming replyRelated recipes
The short answer
Queue a string with ScriptedProvider.EnqueueText, wrap the provider in OneLlmChatClient, and pass that client to the code under test. The bridge package brings in both OneLLM and the Microsoft.Extensions.AI abstractions. You don’t have to change the consumer to take a OneLLM-specific type.
How do you mock Microsoft.Extensions.AI’s IChatClient?
Keep your application code on the Microsoft.Extensions.AI IChatClient interface. In a unit test, construct the fake and pass it to the same method or constructor where production passes the real client. If your app gets its client through dependency injection, use the fake in the test’s composition instead of registering it alongside the production client. That lets you test your app’s response handling without replacing the method that handles it.
Choose a reply that exercises the branch you care about. In the full program, the fake deliberately returns READY, so the test checks that the consumer normalizes whitespace and case instead of simply forwarding the raw response. An empty string can exercise a fallback branch if your app has one. If the request itself matters, check fake.Requests too: the program uses Single() so an unexpected number of requests or messages fails the check even if the final text looks correct.
How do you test streaming chat responses in .NET?
Use GetStreamingResponseAsync for a consumer that reads updates. Enqueue the response just as you do for GetResponseAsync, but assert on what your app builds from the updates rather than what it reads from a single ChatResponse. Create a fresh provider for each independent test so remaining replies or recorded requests from one test don’t affect another. In a multi-turn test, enqueue replies in the order the app is expected to request them; they aren’t matched by prompt text.
The bridge docs describe how OneLLM turns provider messages and events into Microsoft.Extensions.AI responses and updates. The bare-provider constructor shown here uses a test model name rather than resolving a catalog model. That makes it useful for checking what your code sends and how it handles the scripted result. It doesn’t tell you how a live model will answer, whether it will split text at the same boundaries, or whether a deployment accepts the request.