Pester mocking azure cmdlets

Welcome Forums Pester Pester mocking azure cmdlets

  • This topic has 2 replies, 2 voices, and was last updated 1 year ago by
    Senior Moderator
    .
Viewing 2 reply threads
  • Author
    Posts
    • #164241
      Naw
      Participant
      Topics: 4
      Replies: 45
      Points: 91
      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: 91
      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: 9
      Replies: 1243
      Points: 4,475
      Helping Hand
      Rank: Community Hero

      Thanks for sharing the update/solution here.

Viewing 2 reply threads
  • The topic ‘Pester mocking azure cmdlets’ is closed to new replies.