Skip to main content

PowerShell + Pester : counter based mocking

Recently, I have been writing/ reading a lot of Pester tests (both Unit and Integration) for infrastructure validation.

One of the classic limitation hit during mocking with Pester is that you can have different mocks based on different arguments to a parameter (e.g using parameterFilter with Mock ) but not based on a counter.

For Example - See below, I have two mocks for Get-Process cmdlet based on the name passed to it.



Mock -CommandName Get-Service -ParameterFilter {$Name -eq 'winrm'} -mockwith {[PSCustomObjet]@{Status='Running'}}
Mock -CommandName Get-Service -ParameterFilter {$Name -eq 'bits'} -mockwith {[PSCustomObjet]@{Status='Stopped'}


This is really helpful, but there is a case where we want different mocks to occur based on a an incremental counter (number of times) a function/Cmdlet etc. are called in our script.





A very basic function (bad example) illustrating the point where this might be needed is below :



001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
function EnsureServiceStarted {                                                                
param($Name)
    $Service = Get-Service -name $name
    if ($Service.Status -eq 'Running') {
        return $true
    } else {
        Start-Service -name $name
        # check if the service is started
        $Service = Get-Service -Name $name
        if($Service.Status -eq 'Running'){
            return $true
        } else {
            return $false
        }
    }
}

Note that the above function calls Get-Service twice (line 3 & line 9). 

  1. Line 3, the code fetches the current service controller object using Get-Service and then checks if the status is equal to 'Running'. If it is then returns true else it tries to start it.
  2. Line 9 , after trying to start the service, Get-Service cmdlet is used again to fetch the updated status property on it.
All simple and easy right!


But how do you mock Get-Service with pester for testing the logic of starting a stopped service.
It seems you can get a bit crafty, with Pester & PowerShell and do a counter based mocking.

You create a script scope counter and while mocking put the logic in scriptblock passed to the -MockWith parameter.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033

Describe 'EnsureServiceStarted' -Tags Counter {

    Context 'Starting a Stopped service' {
     
        # Arrange
        $Script:counterGetService = 1
        Mock -CommandName Get-Service -MockWith {
            if ($Script:counterGetService -eq 1){ # counter eq 1
                $Script:counterGetService++
                @{Status = 'Stopped'}
            }
            else {
                @{Status = 'Running'}
            }
        }
        Mock -CommandName Start-Service -MockWith {}

        # Act
        EnsureServiceStarted -Name WinRM
     
        # Assert
        It 'Should call the Get-Service twice' { # Get-Service called twice within the function
            $Script:counterGetService | Should be 2
            Assert-MockCalled -CommandName Get-Service -Times 2 -Exactly -Scope Context
        }

        It 'Should call Start-Service once' {
            Assert-MockCalled -CommandName Start-Service -Times 1 -Exactly -Scope Context
        }
    }

}


Now with this very crude counter based mocking, my unit tests pass.


Well it is really up to your creativity on how you want to push the code coverage ;)


Popular posts from this blog

Test connectivity via a specific network interface

Recently while working on a Private cloud implementation, I came across a scenario where I needed to test connectivity of a node to the AD/DNS via multiple network adapters.  Many of us would know that having multiple network routes is usually done to take care of redundancy. So that if a network adapter goes down, one can use the other network interface to reach out to the node. In order to make it easy for everyone to follow along, below is an analogy for the above scenario: My laptop has multiple network adapters (say Wi-Fi and Ethernet) connected to the same network. Now how do I test connectivity to a Server on the network only over say Wi-Fi network adapter?

PowerShell + SCCM : Run CM cmdlets remotely

Today I saw a tweet about using implicit remoting to load the Configuration Manager on my machine by Justin Mathews . It caught my eye as I have never really tried it, but theoretically it can be done. Note - The second tweet says "Cannot find a provider with the name CMSite", resolution to which is in the Troubleshooting section at the end.

PowerShell + WPF + GUI : Hide (Use) background PowerShell Console

Few years back, I had started wrapping my PowerShell scripts with some sort of GUI built using Windows Forms (used Primal Forms CE mostly). Things went fine for a while but then I stumbled across awesome posts by MVP Boe Prox on using WPF with PowerShell to do the same. (check Resources section) I had been procrastinating the idea of playing with WPF for a while but then had a great discussion with MVP Chendrayan (Chen) and got inspired to do it. One can use Visual Studio (Express Edition - which is free) to design the UI and then consume the XAML in PowerShell script...Isn't that Cool ! See resources section for links on that. Often when we write the Code to present a nice UI to the end user there is a PowerShell console running in the background. In this post I would like to share a trick to hide/show the background console window. This trick works with both Winforms and XAML. Note - PowerGUI & Visual Studio Express are absolutely FREE ! For the demo o...