From a290f1e2924da5d5a675d1c3d28b0f9cbde02fd5 Mon Sep 17 00:00:00 2001 From: Scott Addie Date: Mon, 24 Jul 2023 10:39:57 -0500 Subject: [PATCH 1/2] Update mocking guidance links --- samples/README.MD | 6 +-- sdk/core/Azure.Core/README.md | 2 +- sdk/core/Azure.Core/samples/Mocking.md | 54 +------------------------- sdk/core/Azure.Core/samples/README.md | 2 +- 4 files changed, 6 insertions(+), 58 deletions(-) diff --git a/samples/README.MD b/samples/README.MD index 28f6eb4c75ffe..5f0d98911027b 100644 --- a/samples/README.MD +++ b/samples/README.MD @@ -22,15 +22,15 @@ Each package folder contains a package-specific `README.md` file. Most of these Each package folder contains a subfolder called `samples` with additional code samples. These samples can be either short programs contained in `*.cs` files, or more complete how-to guides (code samples and some commentary) contained in `*.md` files. You can find shortcuts to main how-to guides in the [**How-To Guides List**](#how-to-guide-list) section below. ## Sample Applications -Sometimes we want to illustrate how several APIs or even packages work together in a context of a more complete program. For these cases, we created sample applications that you can look at, download, compile, and execute. These application samples are located on +Sometimes we want to illustrate how several APIs or even packages work together in a context of a more complete program. For these cases, we created sample applications that you can look at, download, compile, and execute. These application samples are located on [https://docs.microsoft.com/samples/](https://docs.microsoft.com/samples/). ## How-To Guide List -This section lists **main** how-to guides for the most commonly used APIs and most common scenarios, i.e. this section does not attempt to be a complete directory of guides contained in this repository. +This section lists **main** how-to guides for the most commonly used APIs and most common scenarios, i.e. this section does not attempt to be a complete directory of guides contained in this repository. #### General How-To Guides - [How to access **HTTP response details**](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Response.md) -- [How to **mock**](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Mocking.md) +- [How to **mock**](https://learn.microsoft.com/dotnet/azure/sdk/unit-testing-mocking) - [How to configure **retries**](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Configuration.md) - [How to configure **proxies**](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Configuration.md) - How to configure, access, and analyze **logging** information (TODO) diff --git a/sdk/core/Azure.Core/README.md b/sdk/core/Azure.Core/README.md index 5a865c499a004..1b44237cfa018 100644 --- a/sdk/core/Azure.Core/README.md +++ b/sdk/core/Azure.Core/README.md @@ -232,7 +232,7 @@ SecretClient client = mock.Object; KeyVaultSecret secret = client.GetSecret("Name"); ``` -More on mocking in [mocking samples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Mocking.md). +More on mocking in [Unit testing and mocking with the Azure SDK for .NET](https://learn.microsoft.com/dotnet/azure/sdk/unit-testing-mocking). ## Distributed tracing with Application Insights diff --git a/sdk/core/Azure.Core/samples/Mocking.md b/sdk/core/Azure.Core/samples/Mocking.md index b1c8119c09e6f..16c15d14241b7 100644 --- a/sdk/core/Azure.Core/samples/Mocking.md +++ b/sdk/core/Azure.Core/samples/Mocking.md @@ -1,55 +1,3 @@ # Azure.Core mocking samples -**NOTE:** Samples in this file apply only to packages that follow [Azure SDK Design Guidelines](https://azure.github.io/azure-sdk/dotnet_introduction.html). Names of such packages usually start with `Azure`. - -## Creating a mock of the client using Moq - -You can use a combination of model factory and `Mock` class to create a mock of a client: - -```C# Snippet:ClientMock -// Create a mock response -var mockResponse = new Mock(); - -// Create a mock value -var mockValue = SecretModelFactory.KeyVaultSecret( - SecretModelFactory.SecretProperties(new Uri("http://example.com")) -); - -// Create a client mock -var mock = new Mock(); - -// Setup client method -mock.Setup(c => c.GetSecret("Name", null, default)) - .Returns(Response.FromValue(mockValue, mockResponse.Object)); - -// Use the client mock -SecretClient client = mock.Object; -KeyVaultSecret secret = client.GetSecret("Name"); -``` - -## Creating a mock of the method that returns Pageable - -For methods that return instances of `Pageable` or `AsyncPageable`, `[Async]Pageable.FromPages` method can be used to create an instance for test: - -```C# Snippet:ClientMockWithPageable -// Create a client mock -var mock = new Mock(); - -// Create a Page -var deletedValue = SecretModelFactory.DeletedSecret( - SecretModelFactory.SecretProperties(new Uri("http://example.com")) -); -var pageValues = new[] { deletedValue }; -var page = Page.FromValues(pageValues, default, new Mock().Object); - -// Create a mock for the Pageable -var pageable = Pageable.FromPages(new[] { page }); - -// Setup client method that returns Pageable -mock.Setup(c => c.GetDeletedSecrets(default)) - .Returns(pageable); - -// Use the client mock -SecretClient client = mock.Object; -DeletedSecret deletedSecret = client.GetDeletedSecrets().First(); -``` +This guidance has moved to [Unit testing and mocking with the Azure SDK for .NET](https://learn.microsoft.com/dotnet/azure/sdk/unit-testing-mocking). diff --git a/sdk/core/Azure.Core/samples/README.md b/sdk/core/Azure.Core/samples/README.md index ee643d0e9222d..622f65ca4cf0b 100644 --- a/sdk/core/Azure.Core/samples/README.md +++ b/sdk/core/Azure.Core/samples/README.md @@ -16,5 +16,5 @@ description: Samples for the Azure.Core client library - [Long Running Operations](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/LongRunningOperations.md) - [Events](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Events.md) - [Diagnostics](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Diagnostics.md) -- [Mocking](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Mocking.md) +- [Mocking](https://learn.microsoft.com/dotnet/azure/sdk/unit-testing-mocking) - [Protocol Methods](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/ProtocolMethods.md) From b77aacf622d54c925b471e1a178235998688700c Mon Sep 17 00:00:00 2001 From: Scott Addie Date: Mon, 24 Jul 2023 16:55:53 -0500 Subject: [PATCH 2/2] Delete unused code snippet --- .../tests/samples/MockingSamples.cs | 30 ------------------- 1 file changed, 30 deletions(-) diff --git a/sdk/core/Azure.Core/tests/samples/MockingSamples.cs b/sdk/core/Azure.Core/tests/samples/MockingSamples.cs index b8b2d21e26755..45c441ebe5fab 100644 --- a/sdk/core/Azure.Core/tests/samples/MockingSamples.cs +++ b/sdk/core/Azure.Core/tests/samples/MockingSamples.cs @@ -33,39 +33,9 @@ public void ClientMock() // Use the client mock SecretClient client = mock.Object; KeyVaultSecret secret = client.GetSecret("Name"); - #endregion Assert.NotNull(secret); } - - [Test] - public void ClientMockWithPageable() - { - #region Snippet:ClientMockWithPageable - // Create a client mock - var mock = new Mock(); - - // Create a Page - var deletedValue = SecretModelFactory.DeletedSecret( - SecretModelFactory.SecretProperties(new Uri("http://example.com")) - ); - var pageValues = new[] { deletedValue }; - var page = Page.FromValues(pageValues, default, new Mock().Object); - - // Create a mock for the Pageable - var pageable = Pageable.FromPages(new[] { page }); - - // Setup client method that returns Pageable - mock.Setup(c => c.GetDeletedSecrets(default)) - .Returns(pageable); - - // Use the client mock - SecretClient client = mock.Object; - DeletedSecret deletedSecret = client.GetDeletedSecrets().First(); - #endregion - - Assert.AreEqual(deletedSecret, deletedValue); - } } }