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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<code>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; } } </code> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<code>[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()); } </code> |
Key Points:
Isolate.Fake.NextInstance
: Fakes the next instance ofIEmbeddingGenerator
.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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<code>[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); } </code> |
Key Points:
Isolate.Fake.AllInstances
: Ensures all instances ofIEmbeddingGenerator
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?
- Mocking Complex Dependencies
- Typemock handles non-virtual methods, static methods, and third-party libraries seamlessly.
In this case we faked a future instance of a class that implements our interface
- Typemock handles non-virtual methods, static methods, and third-party libraries seamlessly.
- Testing Edge Cases
- Simulate various scenarios, such as exceptions or unusual inputs, without relying on real AI services.
- Minimized Refactoring
- No need to rewrite code for testability, making Typemock ideal for modern AI-driven and legacy systems alike.
- Verification
- Ensure expected behaviors are invoked, and dependencies interact correctly during execution.
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.