How to test a Cmdlet that Uses SupportsShouldProcess for User Confirmation?

284 views
Skip to first unread message

jck...@gmail.com

unread,
May 28, 2016, 3:27:52 PM5/28/16
to Pester
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

}

Dave Wyatt

unread,
May 28, 2016, 9:15:44 PM5/28/16
to jck...@gmail.com, Pester
Hrm... that is a great question.  We can't mock .NET methods like $PSCmdlet.ShouldProcess().  Normally I recommend that if you want to mock that sort of thing, then you need to wrap the call to the .NET method in a PowerShell function (which you can then mock.)  For some reason that feels weird to do for ShouldProcess(), but it should work.

On Sat, May 28, 2016 at 3:23 PM, <jck...@gmail.com> wrote:
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 have I pass an answer back so more testing can be done with in this one test.

Example code:


function Remove-TestObject{
     [CmdletBinding(SupportsShouldProcess = $true)]
     param(
          [Parameter(Mandatory = $true)]
          [String] $ObjectToDelete,
          [Parameter(Mandatory = $false)]

          [Parameter(Mandatory = $false)]
          [Switch] $Force
     )

     $CheckStatusofSomething = $false

     if ( $Force -or PSCmdlet.ShouldProcess($ObjectToDelete))
     {
           if ( -not $Force -and (-not $CheckStatusofSomething 

}

--
You received this message because you are subscribed to the Google Groups "Pester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pester+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jck...@gmail.com

unread,
May 28, 2016, 9:38:43 PM5/28/16
to Pester, jck...@gmail.com
Hmm, Ok that would be a way to do it, but I don't think that would be the best way for the cmdlet to function, if anything it would probably be best to break a custom confirmation type check into a separate function and not use ShouldProcess. On the other hand using ShouldProcess is more of a powershell best practice for cmdlets (at least from my understanding). 

Thank you for your reply.
Reply all
Reply to author
Forward
0 new messages