. $PSScriptRoot\..\..\Load-PesterModules.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.' $functionPath = Join-Path -Path $here -ChildPath $sut Write-Host "Overriding SUT: $functionPath" Import-Module $functionPath -Force $moduleForMock = "" Describe "Set-ServiceAccountManagedState" { Context "Ensure that it returns with a warning for no service returned" { Mock -ModuleName $moduleForMock -CommandName Get-Service -MockWith { return $null } Mock -ModuleName $moduleForMock -CommandName Invoke-SCExe -MockWith { $global:LASTEXITCODE = 1 } Mock -ModuleName $moduleForMock -CommandName Write-Error Mock -ModuleName $moduleForMock -CommandName Write-Warning Set-ServiceAccountManagedState "anything" It "Ensures Write-Warning got called once for `$null service" { Assert-MockCalled -CommandName Write-Warning -Times 1 -Scope Context -ModuleName $moduleForMock } It "Ensures Write-Error did not get called for `$null service" { Assert-MockCalled -CommandName Write-Error -Times 0 -Scope Context -ModuleName $moduleForMock } } Context "Ensure that it returns an error for a non-zero exit code" { Mock -ModuleName $moduleForMock -CommandName Get-Service -MockWith { return @{}; } #return a non-null value Mock -ModuleName $moduleForMock -CommandName Invoke-SCExe -MockWith { $global:LASTEXITCODE = 1 } Mock -ModuleName $moduleForMock -CommandName Write-Error Mock -ModuleName $moduleForMock -CommandName Write-Warning Set-ServiceAccountManagedState "anything" It "Ensures Write-Warning got called once for `$null service" { Assert-MockCalled -CommandName Write-Warning -Times 0 -Scope Context -ModuleName $moduleForMock } It "Ensures Write-Error did not get called for `$null service" { Assert-MockCalled -CommandName Write-Error -Times 1 -Scope Context -ModuleName $moduleForMock } } }