Skip to content

Commit c0e88b6

Browse files
authored
[AppConfig] Update mocking guidance instructions (#38118)
[AppConfig] Update mocking guidance instructions
1 parent 2809501 commit c0e88b6

File tree

4 files changed

+4
-123
lines changed

4 files changed

+4
-123
lines changed

sdk/appconfiguration/Azure.Data.AppConfiguration/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ Several App Configuration client library samples are available to you in this Gi
233233
* [Get a setting if changed](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/appconfiguration/Azure.Data.AppConfiguration/samples/Sample5_GetSettingIfChanged.md): Save bandwidth by using a conditional request to retrieve a setting only if it is different from your local copy.
234234
* [Update a setting if it hasn't changed](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/appconfiguration/Azure.Data.AppConfiguration/samples/Sample6_UpdateSettingIfUnchanged.md): Prevent lost updates by using optimistic concurrency to update a setting only if your local updates were applied to the same version as the resource in the configuration store.
235235
* [Configuration settings snapshot](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/appconfiguration/Azure.Data.AppConfiguration/samples/Sample11_SettingsSnapshot.md): Create, retrieve and update status of a configuration settings snapshot.
236-
* [Create a mock client](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/appconfiguration/Azure.Data.AppConfiguration/samples/Sample7_MockClient.md): Mock a client for testing using the [Moq library][moq].
236+
* [Create a mock client](https://learn.microsoft.com/dotnet/azure/sdk/unit-testing-mocking): Mock a client for testing.
237237

238238
For more details see the [samples README][samples_readme].
239239

@@ -270,7 +270,6 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
270270
[nuget]: https://www.nuget.org/
271271
[package]: https://www.nuget.org/packages/Azure.Data.AppConfiguration/
272272
[samples_readme]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/appconfiguration/Azure.Data.AppConfiguration/samples/README.md
273-
[moq]: https://github.com/Moq/moq4/
274273
[cla]: https://cla.microsoft.com
275274
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
276275
[code_of_conduct_faq]: https://opensource.microsoft.com/codeofconduct/faq/

sdk/appconfiguration/Azure.Data.AppConfiguration/samples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ description: Samples for the Azure.Data.AppConfiguration client library
1717
- [Read Revision History](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/appconfiguration/Azure.Data.AppConfiguration/samples/Sample4_ReadRevisionHistory.md)
1818
- [Get a Configuration Setting](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/appconfiguration/Azure.Data.AppConfiguration/samples/Sample5_GetSettingIfChanged.md)
1919
- [Update a Configuration If Unchanged](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/appconfiguration/Azure.Data.AppConfiguration/samples/Sample6_UpdateSettingIfUnchanged.md)
20-
- [Mock a client for testing using the Moq library](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/appconfiguration/Azure.Data.AppConfiguration/samples/Sample7_MockClient.md)
20+
- [Mock a client for testing](https://learn.microsoft.com/dotnet/azure/sdk/unit-testing-mocking)
2121
- [Reacting to setting change notification](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/appconfiguration/Azure.Data.AppConfiguration/samples/Sample8_ChangeNotification.md)
2222
- [Create, Retrieve and Delete a Feature Flag](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/appconfiguration/Azure.Data.AppConfiguration/samples/Sample9_FeatureFlags.md)
2323
- [Create, Retrieve and Delete a Secret Reference](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/appconfiguration/Azure.Data.AppConfiguration/samples/Sample10_SecretReference.md)
Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,3 @@
1-
# Mock a client for testing using the Moq library
1+
# Mock a client for testing
22

3-
This sample illustrates how to use [Moq](https://github.com/Moq/moq4/) to create a unit test that mocks the response from a ConfigurationClient method. For more examples of mocking, see the [Azure.Data.AppConfiguration.Tests](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/appconfiguration/Azure.Data.AppConfiguration/tests/ConfigurationMockTests.cs) project.
4-
5-
## Define method that uses `ConfigurationClient`
6-
7-
To show the usage of mocks, define a method that will be tested with mocked objects. For more details about this sample method, see "[Update a Configuration If Unchanged](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/appconfiguration/Azure.Data.AppConfiguration/samples/Sample6_UpdateSettingIfUnchanged.md)" sample.
8-
9-
```C# Snippet:AzConfigSample7_MethodToTest
10-
private static async Task<int> UpdateAvailableVmsAsync(ConfigurationClient client, int releasedVMs, CancellationToken cancellationToken)
11-
{
12-
while (!cancellationToken.IsCancellationRequested)
13-
{
14-
ConfigurationSetting setting = await client.GetConfigurationSettingAsync("available_vms", cancellationToken: cancellationToken);
15-
var availableVmsCount = int.Parse(setting.Value);
16-
setting.Value = (availableVmsCount + releasedVMs).ToString();
17-
18-
try
19-
{
20-
ConfigurationSetting updatedSetting = await client.SetConfigurationSettingAsync(setting, onlyIfUnchanged: true, cancellationToken);
21-
return int.Parse(updatedSetting.Value);
22-
}
23-
catch (RequestFailedException e) when (e.Status == 412)
24-
{
25-
}
26-
}
27-
28-
cancellationToken.ThrowIfCancellationRequested();
29-
return 0;
30-
}
31-
```
32-
33-
## Create and setup mocks
34-
35-
For this test, create a mock for the `ConfigurationClient` and `Response`.
36-
37-
```C# Snippet:AzConfigSample7_CreateMocks
38-
var mockResponse = new Mock<Response>();
39-
var mockClient = new Mock<ConfigurationClient>();
40-
```
41-
42-
Then, set up the client methods that will be executed when `GetConfigurationSettingAsync` and `SetConfigurationSettingAsync` are called on the mock client.
43-
44-
```C# Snippet:AzConfigSample7_SetupMocks
45-
Response<ConfigurationSetting> response = Response.FromValue(ConfigurationModelFactory.ConfigurationSetting("available_vms", "10"), mockResponse.Object);
46-
mockClient.Setup(c => c.GetConfigurationSettingAsync("available_vms", It.IsAny<string>(), It.IsAny<CancellationToken>()))
47-
.Returns(Task.FromResult(response));
48-
mockClient.Setup(c => c.SetConfigurationSettingAsync(It.IsAny<ConfigurationSetting>(), true, It.IsAny<CancellationToken>()))
49-
.Returns((ConfigurationSetting cs, bool onlyIfUnchanged, CancellationToken ct) => Task.FromResult(Response.FromValue(cs, new Mock<Response>().Object)));
50-
```
51-
52-
## Use mocks
53-
54-
Now to validate `ReleaseVmsAsync` without making a network call use `ConfigurationClient` mock.
55-
56-
```C# Snippet:AzConfigSample7_UseMocks
57-
ConfigurationClient client = mockClient.Object;
58-
int availableVms = await UpdateAvailableVmsAsync(client, 2, default);
59-
Assert.AreEqual(12, availableVms);
60-
```
3+
Please refer to the provided guidance for detailed instructions: [Unit testing and mocking with the Azure SDK for .NET](https://learn.microsoft.com/dotnet/azure/sdk/unit-testing-mocking).

sdk/appconfiguration/Azure.Data.AppConfiguration/tests/samples/Sample7_MockClient.cs

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)