Skip to content

Commit ee2cb3f

Browse files
committed
draft
1 parent 10dded8 commit ee2cb3f

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

docs/azure/sdk/aspnetcore-guidance.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ The full order and locations in which `DefaultAzureCredential` looks for credent
231231
- Workload identity
232232
- Managed identity
233233

234-
## Logging
234+
## Configure logging
235235

236236
The Azure SDK for .NET client libraries include the ability to log client library operations. This logging allows you to monitor requests and responses between services clients and Azure services. When you register the Azure SDK library's client via a call to the <xref:Microsoft.Extensions.Azure.AzureClientServiceCollectionExtensions.AddAzureClients%2A> extension method, some logging configurations are handled for you.
237237

@@ -276,3 +276,38 @@ You can change default log levels and other settings using the same JSON configu
276276
}
277277
}
278278
```
279+
280+
## Unit testing considerations
281+
282+
Unit testing is an important part of a sustainable development process that can improve code quality and prevent regressions or bugs in your apps. This section provides a basic introduction to Unit Testing with the Azure SDK for .NET. Visit the [Unit testing and mocking with the Azure SDK for .NET](/dotnet/azure/sdk/unit-testing-mocking) article for a detailed exploration of these unit testing concepts.
283+
284+
Unit testing presents challenges when the code you're testing performs network calls, such as those made to Azure resources by Azure service clients. Tests that run against live services can experience issues, such as latency that slows down test execution, dependencies on code outside of the isolated test, and issues with managing service state and costs every time the test is run. Instead of testing against live Azure services, replace the service clients with mocked or in-memory implementations to avoid these issues.
285+
286+
Each of the Azure SDK clients follows [mocking guidelines](https://azure.github.io/azure-sdk/dotnet_introduction.html#dotnet-mocking) that allow their behavior to be overridden:
287+
288+
* Each client offers at least one protected constructor to allow inheritance for testing.
289+
* All public client members are virtual to allow overriding.
290+
291+
To create a test service client, you can either use a mocking library or standard C# features such as inheritance. Mocking frameworks allow you to simplify the code that you must write to override member behavior. (These frameworks also have other useful features that are beyond the scope of this article.)
292+
293+
```csharp
294+
KeyVaultSecret keyVaultSecret = SecretModelFactory.KeyVaultSecret(
295+
new SecretProperties("secret"), "secretValue");
296+
297+
Mock<SecretClient> clientMock = new Mock<SecretClient>();
298+
clientMock.Setup(c => c.GetSecret(
299+
It.IsAny<string>(),
300+
It.IsAny<string>(),
301+
It.IsAny<CancellationToken>())
302+
)
303+
.Returns(Response.FromValue(keyVaultSecret, Mock.Of<Response>()));
304+
305+
clientMock.Setup(c => c.GetSecretAsync(
306+
It.IsAny<string>(),
307+
It.IsAny<string>(),
308+
It.IsAny<CancellationToken>())
309+
)
310+
.ReturnsAsync(Response.FromValue(keyVaultSecret, Mock.Of<Response>()));
311+
312+
SecretClient secretClient = clientMock.Object;
313+
```

0 commit comments

Comments
 (0)