Skip to content

Latest commit

 

History

History
27 lines (18 loc) · 1.35 KB

File metadata and controls

27 lines (18 loc) · 1.35 KB
title Unit testing Kiota API clients
description Learn about writing unit tests for Kiota API clients.
author LockTar
ms.author vibiret
ms.topic conceptual
ms.date 07/04/2023

Unit testing Kiota API clients

Unit testing API clients against the actual API can often be impractical. Issues like network failures, authentication requirements, or changing data can make testing difficult or impossible. We recommend that unit tests use mock implementations of the HTTP transport layer to control API responses.

For Kiota API clients, the HTTP layer is contained in a request adapter. Mocking the request adapter allows you to control API responses.

Prerequisite

In this sample, the following frameworks/tools are used.

  • xUnit - unit testing framework for .NET
  • NSubstitute - mocking library to mock interfaces

Kiota client unit testing

The following unit tests are for an API client generated for the /posts endpoint in the JSONPlaceholder REST API. The full project can be downloaded from GitHub.

:::code language="csharp" source="~/code-snippets/get-started/quickstart/dotnet/test/PostsRequestsTests.cs" id="UnitTestsSnippet":::