. $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 "Disable-Radium" -Tag 'Unit' { Mock -CommandName "Disable-Service" -MockWith {} -ModuleName $moduleForMock <# Pester `Mock -MockWith` is ... weird. It lets you use parameter variable names from *inside* the scope of the namespace you're mocking the command call *from*. So $sName below is not evaluated until the call to Stop-AlkamiService from *inside* of Alkami.PowerShell.Services happens. At which point, $sName has a value https://github.com/pester/Pester/wiki/Mock - where they describe the `Mock-With` parameter. #> Mock -CommandName "Stop-AlkamiService" -MockWith { return $sName } -ModuleName $moduleForMock Mock -CommandName "Get-Service" { New-MockObject -Type System.ServiceProcess.ServiceController } -ModuleName $moduleForMock It "calls Disable-Service with ServiceObject" { Disable-Radium Assert-MockCalled -ModuleName $moduleForMock -CommandName Disable-Service -ParameterFilter { $service } -Scope It -Exactly 1 } It "calls Stop-AlkamiService with 'Alkami Radium Scheduler Service'" { Disable-Radium Assert-MockCalled -ModuleName $moduleForMock -CommandName Stop-AlkamiService -ParameterFilter { $sName -eq 'Alkami Radium Scheduler Service' } -Scope It -Exactly 1 } It "does nothing if Radium not found" { Mock -CommandName "Get-Service" {return $null} -ModuleName $moduleForMock Disable-Radium Assert-MockCalled -ModuleName $moduleForMock -CommandName Disable-Service -ParameterFilter { $service } -Scope It -Exactly 0 Assert-MockCalled -ModuleName $moduleForMock -CommandName Stop-AlkamiService -ParameterFilter { $sName -eq 'Alkami Radium Scheduler Service' } -Scope It -Exactly 0 } }