-
-
Notifications
You must be signed in to change notification settings - Fork 469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to mock commands like azure cli #1883
Comments
The big difference is that your native commands won't have parameters, so you need to rely on the
This should ideally be moved to https://github.com/pester/docs as the example would probably be best suited in the usage-guide, https://pester.dev/docs/usage/mocking . |
Ok, that is exactly what I was looking for. Yes would be great to update the docs |
Hello, I would like to mock You will find below more details regarding what I am trying to do.
For example, I would like to validate the behavior of my function based on the number of items in the output of the call to the Do you think there is today a way to do that with Pester? |
Yes. Just replace the empty
If there are multiple calls to |
Hey @fflaten, Thank you very much for your help. |
I sort of have a similar issue - but it's more to do with using Pester in Azure Pipelines (using v5.1.1 Pester). I have this sort of mock for an Azure CLI command Get-AzSubscription -
Where Get-AzSubscription is used in HasAccessToSubscription. To be able to get it to work, I have to import in my pipeline: For any Azure command I want to mock and check - I have to install the module in my pipeline? |
This isn't related to the native cli mock issue. For future support questions see Where to get support 👍 The point of mocks is to imitate the existing method/function with the exact interface, but with controlled output and no external dependencies or danger/cost associated with executing the code. So mocks by-design require the original function to be available. However, there's nothing stopping you from faking the original if it doesn't exist, but you'd have to provide your "original" with the expected parameters. Remember that at this point you are no longer testing against the public api/interface for Describe 'Mock sample' {
It 'Mocking unavailable function' {
if($null -eq (Get-Command Get-AzSubscription -ErrorAction SilentlyContinue)) { function Get-AzSubscription ($SubscriptionId) { } }
Mock Get-AzSubscription { Write-Warning "Mock my world!" }
function HasAccessToSubscription { Get-AzSubscription -SubscriptionId 123 }
HasAccessToSubscription
Assert-MockCalled -CommandName Get-AzSubscription -Times 1 -Exactly
}
} |
I tried to find in the docs how to mock commands like
Azure Cli
For instance I have the following code:
How can I mock
az ad app create
and assert the parameters withAssert-MockCalled
? I don't find anything similar on the docs. Could you please update it with some example? Or should I assume this is not possible and I should create a wrapper for every Azure cli command? I don't want to rely on Azure AD ModuleThe text was updated successfully, but these errors were encountered: