forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample7_MockClient.cs
36 lines (31 loc) · 1.41 KB
/
Sample7_MockClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Threading;
using Moq;
using NUnit.Framework;
namespace Azure.Data.AppConfiguration.Samples
{
public partial class ConfigurationSamples
{
[Test]
/*
* This sample illustrates how to use Moq to create a unit test that
* mocks the reponse from a ConfigurationClient method. For more
* examples of mocking, see the Azure.Data.AppConfiguration.Tests project.
*/
public void MockClient()
{
// Create a mock response.
var mockResponse = new Mock<Response>();
// Create a mock client.
var mockClient = new Mock<ConfigurationClient>();
// Set up a client method that will be called when GetConfigurationSetting is called on the mock client.
mockClient.Setup(c => c.GetConfigurationSetting("Key", It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(Response.FromValue(ConfigurationModelFactory.ConfigurationSetting("Key", "Value"), mockResponse.Object));
// Use the mock client to validate client functionality without making a network call.
ConfigurationClient client = mockClient.Object;
ConfigurationSetting setting = client.GetConfigurationSetting("Key");
Assert.AreEqual("Value", setting.Value);
}
}
}