Pester mocking azure cmdlets

Welcome Forums Pester Pester mocking azure cmdlets

This topic contains 2 replies, has 2 voices, and was last updated by

 
Senior Moderator
4 months ago.

  • Author
    Posts
  • #164241
    Naw

    Participant
    Topics: 4
    Replies: 45
    Points: 90
    Rank: Member

    Hello
    I am trying out Pester for the unit testing. Is there better another way to test this or am I doing completely wrong?
    Here is the function I need to test for unit testing.

    Function Add-SubnetToVNet{
        Param(        
            [Parameter(Mandatory)]$SubnetName,        
            [Parameter(Mandatory)]$AddressPrefix,
            [Parameter(Mandatory)]$ResourceGroupName,
            [Parameter(Mandatory)]$VNetName
        )
        Process{
            $PSVnet = Get-AzVirtualNetwork -ResourceGroupName $ResourceGroupName -Name $VNetName
            Add-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $AddressPrefix -VirtualNetwork $PSVnet        
            Set-AzVirtualNetwork -VirtualNetwork $PSVnet
        }
    }
    

    Here is the Pester test describe block.

    Describe 'Add-SubnetToVNet'{
    	Mock -CommandName 'Get-AzVirtualNetwork' -MockWith{
    		$VnetObj = New-Object Microsoft.Azure.Commands.Network.Models.PSVirtualNetwork
    		$VnetObj.Name              = 'VNet-Name'
    		$VnetObj.ResourceGroupName = 'RG-VNet-Name'
    		$VnetObj.Location          = 'uksouth'           
    		return $VnetObj
    	}
    	Mock -CommandName 'Add-AzVirtualNetworkSubnetConfig'-MockWith{            
    		$SubnetObj = New-Object Microsoft.Azure.Commands.Network.Models.PSSubnet
    		$SubnetObj.Name = 'New-Subnet'
    		$SubnetObj.Id   = '/subcriptions/random-guid-here/resourceGroups/RG-VNet-Name/Subnet-Name'
    		$SubnetObj.ProvisioningState = 'Succeeded'
    		$SubnetObj.AddressPrefix     = '10.7.0.0/24'
    		return ($SubnetObj)
    	}
    	Mock -CommandName 'Set-AzVirtualNetwork' -MockWith{	
    		$VnetObj.Subnets.Add($SubnetObj)          
    		return $VnetObj
    	}
    	$TestVnet = Add-SubnetToVNet -SubnetName 'New-Subnet' -AddressPrefix '10.7.0.0/24' -ResourceGroupName 'RG-VNet-Name' -VNetName 'VNet-Name'
    	It 'Should return a VNet with a subnet'{
    		$TestVnet = Add-SubnetToVNet -SubnetName 'New-Subnet' -AddressPrefix '10.7.0.0/24' -ResourceGroupName 'RG-VNet-Name' -VNetName 'VNet-Name'            
    		$TestVnet.Subnets | Should Not Be $null
    	}
    }
    

    It said the virtual network was null and the test failed.
    Thanks.

  • #165280
    Naw

    Participant
    Topics: 4
    Replies: 45
    Points: 90
    Rank: Member

    I have got this now. Just follow the pattern on the mock from this link.
    https://4sysops.com/archives/mocking-with-pester-in-powershell-unit-testing/

  • #165283

    Senior Moderator
    Topics: 8
    Replies: 1118
    Points: 3,806
    Helping Hand
    Rank: Community Hero

    Thanks for sharing the update/solution here.

The topic ‘Pester mocking azure cmdlets’ is closed to new replies.