| 
1 |  | -# Mock a client for testing using the Moq library  | 
 | 1 | +# Mock a client for testing  | 
2 | 2 | 
 
  | 
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).  | 
0 commit comments