Logo

dev-resources.site

for different kinds of informations.

Is there any option for Mock EncryptAsync() in Azure.Security.KeyVault.Keys.Cryptography

Published at
6/11/2024
Categories
azure
keyvault
dotnetcore
mock
Author
manu_vr
Categories
4 categories in total
azure
open
keyvault
open
dotnetcore
open
mock
open
Author
7 person written this
manu_vr
open
Is there any option for Mock EncryptAsync() in Azure.Security.KeyVault.Keys.Cryptography

I am trying to mock the functionality of the EncryptAsync method in Azure.Security.KeyVault.Keys.Cryptography.CryptographyClient.

This is the method I am trying to cover in my unit test.

public KeyVaultClientWrapper(KeyClient keyClient, SecretClient secretClient, CryptographyClient cryptographyClient)
    {
        _keyClient = keyClient;
        _cryptographyClient = cryptographyClient;
        _secretClient = secretClient;
    }

    public async Task<EncryptResult> EncryptAsync(string keyName, string algorithm, byte[] plainText, CancellationToken cancellationToken = default)
    {
        return await _cryptographyClient.EncryptAsync(algorithm, plainText, cancellationToken);
    }
Enter fullscreen mode Exit fullscreen mode

I'm unable to create new object of EncryptResult that needs to returned from the mock EncryptAsync method because there is no Set for EncryptResult

var _mockKeyVaultClient = new Mock<IKeyVaultClient>();
    var _mockCryptoClient = new Mock<CryptographyClient>();
    mockCryptoClient
            .Setup(client => client.EncryptAsync(EncryptionAlgorithm.AES, plaintext, default))
            .ReturnsAsync(new EncryptResult { Ciphertext = ciphertext });
Enter fullscreen mode Exit fullscreen mode

How can I change this mock class so that EncryptAsync returns a valid EncryptResult object while running the test method?

Is there any alternate option for testing the same?

.NETCore

mock Article's
30 articles in total
Favicon
FAQ β€” Bloomer Mock Data Generator
Favicon
Best Practices: How to Make Your API Smarter and More Flexible
Favicon
Testing ReactJS Context - A Guide with test-doubles
Favicon
The beauty of MSW
Favicon
Realistic data Generation Using Bloomer Mock
Favicon
Testing Spring Boot Applications: Unit, Integration, and Mocking β€” A Comprehensive Guide
Favicon
How to debug your Python mocks or imports
Favicon
Is there any option for Mock EncryptAsync() in Azure.Security.KeyVault.Keys.Cryptography
Favicon
Learn to Simulate Online APIs Without Server Infrastructure
Favicon
Diferenças entre o jest.spyOn e jest.mock
Favicon
Mock Class Constructor in Jest Test with Mocking Partials
Favicon
You stub/mock incorrectly
Favicon
Step-by-Step Tutorial on Setting Up a Mock Server Using Postman
Favicon
Mocking ES6 Class Instances with Jest
Favicon
Create free mock apis with unlimited storage
Favicon
Free API server with unlimited access
Favicon
Handle Variables and Dynamic Values with `jest.fn()`
Favicon
A way to mock PHP internal functions with xepozz/internal-mocker
Favicon
Mock vs Stub vs Fake: Understand the difference
Favicon
Mock modules properly with Jest and Typescript
Favicon
Simplified Strategies for Mocking API Calls
Favicon
Fastify Meets WireMock: External Service Mocking
Favicon
OpenAI API Mock for Devs on a Budget
Favicon
Mock S3 for AWS SDK for JavaScript (v3)
Favicon
Simplifying Kafka Testing in Python: A Mockafka-py Tutorial
Favicon
When to use Mock API?
Favicon
Datafaker: Simplifying Test Data Generation for Java and Kotlin
Favicon
NestJS: Mocking Databases for Efficient Tests
Favicon
Understanding Mocks, Stubs, and Fakes in Software Testing
Favicon
How to Typescript to JSON with Butlermock

Featured ones: