Hi,
I have a Cmdlet that uses ShouldProcess "[CmdletBinding(SupportsShouldProcess = $true)]" to get user confirmation before deleting an object.
I use both ShouldProcess and ShouldContinue statements depending on a set of logic.
Before I get to the code example, how can I create a pester test to say "| Should Confirm " (aka should prompt for user confirmation)? Then how can I pass an answer back so more testing can be done within this one test.
Example code:
function Remove-TestObject{
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true)]
[String] $ObjectToDelete,
[Parameter(Mandatory = $false)]
[Switch] $NoRecycle,
[Parameter(Mandatory = $false)]
[Switch] $Force
)
$CheckStatusofSomething = $false
#first confirmation
if ( $Force -or PSCmdlet.ShouldProcess($ObjectToDelete)) {
#second confirmation
if ( -not $Force -and (-not $CheckStatusofSomething -or $NoRecycle)) {
#write abort
break
}
}
else {
break
}
#Continue with destructive task
}