Dave Richardson
Dave Richardson

Reputation: 3

Trying to mock Restart-AzureRMWebApp continues to fail

I'm setting up unit tests for out script that will restart Azure WebApps. When trying to Mock Restart-AzureRmWebApp the mock does not get called and at times the WebApp will actually restart. I'm running Pester 4.8.1 and PowerShell version PSVersion 5.1.17134.858

I've tried using parameter filters, added return of return New-Object Microsoft.Azure.Management.WebSites.Models.Site

This is code before I started and wanting to wrap the code with Pester before rewriting.

Function

Function Restart-WebApp {
[CmdletBinding()]
Param
    (
    [Parameter(Mandatory=$true)][string]$WebAppName,
    [Parameter(Mandatory=$true)][string]$ResourceGroupName
    )


#restarting web app
{
    Write-Output "Restarting web app: $WebAppName"
    $null = Restart-AzureRmWebApp -ResourceGroupName $ResourceGroupName -Name $WebAppName -ErrorAction Stop
    Write-Output "Checking status"
}

Test

Describe "Restart Webapps Unit Tests" {
    Context "Restart Webapps Unit Test" {
        $WebApp = "our-webapp-dev-westus"
        $Environment = 'dev'
        # Get-ResourceGroup is our internal script to get the Resource Group Name
        $rsg = Get-ResourceGroup -Environment $Environment

        Mock -CommandName Restart-AzureRmWebApp {} #-ParameterFilter { $ResourceGroupName -eq $rsg -and $Name -eq $WebApp} {}#{ return New-Object Microsoft.Azure.Management.WebSites.Models.Site }
        It "should restart Web App" {
            Restart-WebApp -WebAppName $WebApp -ResourceGroupName $rsg
            Assert-MockCalled -CommandName Restart-AzureRmWebApp
        }
    }
}

Results from test

Describing Restart Webapps Unit Tests

    Context Restart Webapps Unit Test
      [-] should restart Web App 327ms
        at <ScriptBlock>, : line 20
        20:             Assert-MockCalled -CommandName Restart-AzureRmWebApp
        Expected Restart-AzureRmWebApp to be called at least 1 times but was called 0 times
Tests completed in 472ms
Tests Passed: 0, Failed: 1, Skipped: 0, Pending: 0, Inconclusive: 0

Upvotes: 0

Views: 90

Answers (1)

Hitesh Kacholiya
Hitesh Kacholiya

Reputation: 38

Results from my run:

Function Restart-WebApp {
[CmdletBinding()]
Param
    (
    [Parameter(Mandatory=$true)][string]$WebAppName,
    [Parameter(Mandatory=$true)][string]$ResourceGroupName
    )
}

#restarting web app
{
    Write-Output "Restarting web app: $WebAppName"
    $null = Restart-AzureRmWebApp -ResourceGroupName $ResourceGroupName -Name $WebAppName -ErrorAction Stop
    Write-Output "Checking status"
}

Describe "Restart Webapps Unit Tests" {
    Context "Restart Webapps Unit Test" {
        $WebApp = "webappname"
        $Environment = 'dev'
        # Get-ResourceGroup is our internal script to get the Resource Group Name
        $rsg = "rgname"

        Mock -CommandName Restart-WebApp {} #-ParameterFilter { $ResourceGroupName -eq $rsg -and $Name -eq $WebApp} {}#{ return New-Object Microsoft.Azure.Management.WebSites.Models.Site }
        It "should restart Web App" {
            Restart-WebApp -WebAppName $WebApp -ResourceGroupName $rsg
            Assert-MockCalled -CommandName Restart-WebApp
        }
    }
}

Result:

Describing Restart Webapps Unit Tests
   Context Restart Webapps Unit Test
    [+] should restart Web App 4.56s

I would recommend you to try out the REST APIs for starting, restarting and stopping the app services. In my experience, they are pretty quick.

Stop: https://learn.microsoft.com/en-us/rest/api/appservice/webapps/stop

Start: https://learn.microsoft.com/en-us/rest/api/appservice/webapps/start

Restart: https://learn.microsoft.com/en-us/rest/api/appservice/webapps/restart

Upvotes: 0

Related Questions