DEV Community

Manu Radhakrishnan
Manu Radhakrishnan

Posted on

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay