Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mocking guidance links #37806

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions samples/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
54 changes: 1 addition & 53 deletions sdk/core/Azure.Core/samples/Mocking.md
Original file line number Diff line number Diff line change
@@ -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
scottaddie marked this conversation as resolved.
Show resolved Hide resolved
// Create a mock response
var mockResponse = new Mock<Response>();

// Create a mock value
var mockValue = SecretModelFactory.KeyVaultSecret(
SecretModelFactory.SecretProperties(new Uri("http://example.com"))
);

// Create a client mock
var mock = new Mock<SecretClient>();

// 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<T>.FromPages` method can be used to create an instance for test:

```C# Snippet:ClientMockWithPageable
// Create a client mock
var mock = new Mock<SecretClient>();

// Create a Page
var deletedValue = SecretModelFactory.DeletedSecret(
SecretModelFactory.SecretProperties(new Uri("http://example.com"))
);
var pageValues = new[] { deletedValue };
var page = Page<DeletedSecret>.FromValues(pageValues, default, new Mock<Response>().Object);

// Create a mock for the Pageable
var pageable = Pageable<DeletedSecret>.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).
scottaddie marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
christothes marked this conversation as resolved.
Show resolved Hide resolved
- [Protocol Methods](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/ProtocolMethods.md)
30 changes: 0 additions & 30 deletions sdk/core/Azure.Core/tests/samples/MockingSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SecretClient>();

// Create a Page
var deletedValue = SecretModelFactory.DeletedSecret(
SecretModelFactory.SecretProperties(new Uri("http://example.com"))
);
var pageValues = new[] { deletedValue };
var page = Page<DeletedSecret>.FromValues(pageValues, default, new Mock<Response>().Object);

// Create a mock for the Pageable
var pageable = Pageable<DeletedSecret>.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);
}
}
}