Test AI

Typemock Isolator 9.3.3: Embracing .NET 9 and C# 13 with AI Testing Capabilities

We are excited to announce the release of Typemock Isolator 9.3.3, the most advanced version yet of our unit testing tool. This release is designed to support the latest innovations in the .NET ecosystem, including .NET 9 and C# 13. Whether you’re exploring AI integration, adopting cutting-edge C# features, or building robust applications, Typemock Isolator provides everything you need to write reliable and maintainable unit tests.


What’s New in Typemock Isolator 9.3.3

1. Full Support for .NET 9

.NET 9 brings significant improvements in performance, cloud-native development, and AI integration. Typemock Isolator 9.3.3 ensures seamless compatibility with .NET 9, allowing you to test modern applications without missing a beat.

2. C# 13 Features for Enhanced Testability

With features like

params
collection enhancements, partial properties, and the
field
keyword, C# 13 simplifies coding. Typemock Isolator fully supports these new capabilities, making it easier to write and test code using modern C# constructs.

3. Simplified AI Testing

The rise of AI-driven applications presents unique testing challenges, particularly when working with dynamic behaviors or external dependencies like AI models and APIs. Typemock Isolator 9.3.3 introduces improved support for mocking AI-related libraries, such as those using Microsoft.Extensions.AI.


Testing AI-Driven Applications with Typemock

To demonstrate the power of Typemock Isolator in testing AI integrations, let’s walk through an example of testing code that generates embeddings using the

Microsoft.Extensions.AI
library.


The Code Under Test

The following

AIService
class interacts with an external embedding generator to process input text and generate embeddings:

using Microsoft.Extensions.AI;

public class AIService
{
    public async Task<GeneratedEmbeddings<Embedding<float>>> checkingAIAsync()
    {
        IEmbeddingGenerator<string, Embedding<float>> generator =
            new OllamaEmbeddingGenerator(new Uri("http://localhost:11434/"), "all-minilm");

        var embedding = await generator.GenerateAsync(new string[] { "What is AI?" });

        return embedding;
    }
}
  • IEmbeddingGenerator
    : An interface for generating embeddings.
  • OllamaEmbeddingGenerator
    : A concrete implementation interacting with the AI model.

Unit Testing AI Code with Typemock

Using Typemock Isolator, we can test the

checkingAIAsync
method by mocking the
IEmbeddingGenerator
interface, allowing us to simulate its behavior without relying on the actual AI service.


Test 1: Simulating an Exception

This test ensures that the method correctly handles exceptions thrown by the embedding generator.

[TestMethod]
public async Task checkingAIAsync_GeneratesEmbeddingWillThrow_ExpectedThrow()
{
    // Arrange
    var generatorMock = Isolate.Fake.NextInstance<IEmbeddingGenerator<string, Embedding<float>>>();

    Isolate.WhenCalled(() => generatorMock.GenerateAsync(new string[] { "What is AI?" }))
           .WillThrow(new Exception("Thrown"));

    var service = new AIService();

    // Act & Assert
    await Assert.ThrowsExceptionAsync<Exception>(async () => await service.checkingAIAsync());
}

Key Points:

  • Isolate.Fake.NextInstance
    : Fakes the next instance of
    IEmbeddingGenerator
    .
  • Isolate.WhenCalled
    : Configures the mocked method to throw an exception.
  • The test ensures the
    AIService
    handles the exception as expected.

Test 2: Simulating a Successful Response

This test validates that the service correctly processes a valid response from the embedding generator.

[TestMethod]
public async Task checkingAIAsync_GeneratesEmbeddingWillReturnNewMockedEmbedding_Works()
{
    // Arrange
    var generatorMock = Isolate.Fake.AllInstances<IEmbeddingGenerator<string, Embedding<float>>>();

    var embedding = new Embedding<float>(new ReadOnlyMemory<float>(new float[] { 0.1f, 0.2f, 0.3f }));
    var generatedEmbeddings = new GeneratedEmbeddings<Embedding<float>>(new[] { embedding  });

    Isolate.WhenCalled(() => generatorMock.GenerateAsync(new string[] { "What is AI?" }))
           .WillReturn(Task.FromResult(mockGeneratedEmbeddings));

    var service = new AIService();

    // Act
    var result = await service.checkingAIAsync();

    // Assert
    Assert.AreEqual(generatedEmbeddings, result);
}

Key Points:

  • Isolate.Fake.AllInstances
    : Ensures all instances of
    IEmbeddingGenerator
    are mocked.
  • Simulates a successful response from the AI service.
  • Verifies that the
    GenerateAsync
    method is called with the expected arguments.

Why Use Typemock Isolator for AI Testing?

  1. Mocking Complex Dependencies
  2. Testing Edge Cases
  3. Minimized Refactoring
  4. Verification

C# 13 Features with Typemock

Typemock Isolator’s support for C# 13 features ensures developers can test modern code constructs with ease:

  • Enhanced
    params
    Collections
    : Test methods using advanced collection handling.
  • Partial Properties: Mock and verify getter and setter behaviors separately.
  • field
    Keyword
    : Simulate property backing field behavior without explicit field definitions.

Upgrade to Typemock Isolator 9.3.3 Today

Typemock Isolator 9.3.3 provides the tools you need to confidently test AI-driven applications, modern C# 13 code, and .NET 9 projects. With Typemock, you can build robust and maintainable tests for even the most complex scenarios.

👉 Download Typemock Isolator 9.3.3
👉 Explore Documentation