C
C#2y ago
bdcp

❔ Writing tests for Minimal API that calls KeyVault and Azure

How can i write tests for a web app using minimal Apis.
internal class UserWebApplication : WebApplicationFactory<Program>
{
protected override IHost CreateHost(IHostBuilder builder)
{
// How can i just override keyvault here?
builder.ConfigureHostConfiguration(config =>
{
config.AddInMemoryCollection(new Dictionary<string, string>
{
["KeyVault:Uri"] = "??",
});
});


return base.CreateHost(builder);
}
}
internal class UserWebApplication : WebApplicationFactory<Program>
{
protected override IHost CreateHost(IHostBuilder builder)
{
// How can i just override keyvault here?
builder.ConfigureHostConfiguration(config =>
{
config.AddInMemoryCollection(new Dictionary<string, string>
{
["KeyVault:Uri"] = "??",
});
});


return base.CreateHost(builder);
}
}
The Docs say i have to use WebApplicationFactory<Program> but i can't create it bescause it tries to call KeyVault and Azure client'd also need to be set etc. Is it possible to just add a mock keyvault into it?
3 Replies
Zendist
Zendist2y ago
Check out this article, that should help you get started: https://devblogs.microsoft.com/azure-sdk/unit-testing-and-mocking/ You should probably mock the KV client object, such that you never actually call Azure KV from your test code. Remember to also have an integration test which does call KV, to ensure you got the glue code right.
Pavel Krymets
Azure SDK Blog
Unit testing and mocking with Azure SDK .NET
Learn how to reliably unit test your code that is using the Azure SDKs.
bdcp
bdcpOP2y ago
hmm thanks for the link . The part of keyvault that needs to be called just adds the values of keyvault to the configuration. Not injecting Keyvault itself. No idea how to mock that
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server