. $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 "Stop-AlkamiService" { Mock -ModuleName $moduleForMock -CommandName Get-LogLeadName -MockWith {} Mock -ModuleName $moduleForMock -CommandName Get-Service -MockWith { $returnObject = [PSCustomObject]@{Status='Running'; Name ='JunkService'} $returnObject | Add-Member -MemberType ScriptMethod -Name Refresh -Value {write-host "setting $($this.Status) to Stopped"; $this.Status = 'Stopped'} -Force $returnObject | Add-Member -MemberType ScriptMethod -Name WaitForStatus -Value {} -Force return $returnObject } Mock -ModuleName $moduleForMock -CommandName Get-ProcessFromService -MockWith {[PSCustomObject]@(@{Id = 123; ProcessName = "MyService"; Path = "Some\Path"})} Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {} Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {} Mock -ModuleName $moduleForMock -CommandName Stop-ProcessIfFound -MockWith {} Context "When Successfully Trying to Stop A Service"{ It "Doesn't Throw"{ {Stop-AlkamiService "junkService" } | Should -Not -Throw } It "Stops"{ $returnValue = Stop-AlkamiService "junkService" # Depending on the $PSCmdlet.ShouldProcess result, either Stopped or True is valid here. This should be set to just Stopped once we wrap ShouldProcess $returnValue | Should -Be ('Stopped' -or $true) } } Context "When Invoke-SCExe throws"{ Mock -ModuleName $moduleForMock -CommandName Invoke-SCExe -MockWith { throw } Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {} # This won't function until we wrap ShouldProcess It "Writes A Warning" -skip { Stop-AlkamiService "junkService" Assert-MockCalled -CommandName Write-Warning -Times 1 -Exactly -Scope It ` -ModuleName $moduleForMock -ParameterFilter { $Message -match "Service failed to stop" } } } }