---
title: "Fake IChatClient responses in .NET tests"
url: https://recursiveai.org/onellm/cookbook/mock-ichatclient-with-scripted-provider/
description: "Fake IChatClient responses in .NET tests with OneLLM’s ScriptedProvider. Run your existing chat code against a fixed reply and check the prompt it sent."
version: "1.0.1"
---

# Fake IChatClient responses in .NET tests

**OneLLM** is a free, open-source LLM and agent library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneLlm.Extensions.AI) · [GitHub](https://github.com/recursive-ai-oss/onellm-csharp) · [Docs](https://recursiveai.org/onellm/docs/) · [Download example (.zip)](https://recursiveai.org/onellm/cookbook/mock-ichatclient-with-scripted-provider/mock-ichatclient-with-scripted-provider.zip)

Use the fake with any code that takes Microsoft.Extensions.AI’s `IChatClient`.

```shell
dotnet add package RecursiveAI.OneLlm.Extensions.AI
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onellm/cookbook/mock-ichatclient-with-scripted-provider/mock-ichatclient-with-scripted-provider.zip), then `dotnet run`.

```text
Ready
```

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

```csharp
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();
}
```

```text
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.

```csharp
fake.EnqueueText("Streaming reply");
await foreach (var update in
    chat.GetStreamingResponseAsync("Status?"))
{
    Console.Write(update.Text);
}
Console.WriteLine();
```

```text
Streaming reply
```

## Related recipes

- [Test OneLLM code without an API key](https://recursiveai.org/onellm/cookbook/test-llm-code-without-api-key/)
- [Stream a chat response in C#](https://recursiveai.org/onellm/cookbook/stream-chat-response/)

## 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](https://learn.microsoft.com/en-us/dotnet/ai/ichatclient). 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](https://recursiveai.org/onellm/docs/extensions-ai/) 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.
