- This topic has 2 replies, 2 voices, and was last updated 1 month, 3 weeks ago by
Participant.
-
AuthorPosts
-
-
November 25, 2020 at 6:54 pm #274209
How would one go about properly mocking ‘Invoke-Command’ for this example? This is just a part of my function that I’m trying to test at the moment, the second part removes the package if there is a match (calling ‘Invoke-Command’ a second time with a different script block)..
PowerShell123456789101112131415function Remove-ChocoPackage{param( [Parameter(Mandatory=$true)]$packageName,[Parameter(Mandatory=$true)]$computerName )$success = $false$listPackages = @(Invoke-Command -ComputerName $computerName -ScriptBlock {choco list -lo})$containsPackage = if(@($listPackages) -eq $packageName){$true}else{$false}if($containsPackage){$success = $true}return $success}The test
PowerShell12345678910Describe 'Test package existence'{$packageName1 = "Package1"$packageName2 = "Package2"$computerName = "comp1234"It 'is it there'{Mock Invoke-Command { return $packageName1,$packageName2 }{ Remove-ChocoP -packageName $packageName1 -computerName $computerName } | Should -Be $trueAssert-MockCalled Invoke-Command -Scope It}}Results in:
Expected Invoke-Command to be called at least 1 times but was called 0 timesAs mentioned earlier, the full function calls ‘Invoke-Command’ a second time. I’m trying to understand how I can Mock ‘Invoke-Command’ for the two different calls, or do I need to even further and Mock ‘choco’?
The full function:
PowerShell123456789101112131415161718192021222324252627282930313233function Remove-ChocoPackage{param([Parameter(Mandatory=$true)]$packageName,[Parameter(Mandatory=$true)]$computerName)$success = $false$listPackages = @(Invoke-Command -ComputerName $computerName -ScriptBlock {choco list -lo})$containsPackage = if(@($listPackages) -eq $packageName){$true}else{$false}if($containsPackage){$removeStatus = invoke-command -ComputerName $computerName -ScriptBlock {choco uninstall $args[0] -y | out-nullif($?){return $true}else{return $false}} -argumentList $packageNameif($removeStatus){$success = $true}}else{$success = $true}return $success} -
November 27, 2020 at 12:20 am #274518
you don’t need to wrap Remove-ChocoP -packageName $packageName1 -computerName $computerName inside {}. That is required only when testing exceptiions.
And there is a typo as your function name is Remove-ChocoPackage but you typed it as Remove-ChocoP, but that was not caught as the error occurred inside {} context I think. Mocking works fine if you remove {} and correct the typo. -
November 27, 2020 at 5:59 pm #274710
Actually tried to remove all that after posting, was just a typo…
With the typo fixed and the curly braces removed, still experienced issues with ‘invoke-command’.
A couple of things needed to be resolved…
First, realized I had my repo in a folder under ‘my documents’… moved this all to a shorter/simpler path…
Also, ended up breaking out the first ‘invoke-command’ out into a private function, which I then mocked… which solved my issues…
-
-
AuthorPosts
- The topic ‘Mocking Invoke-Command’ is closed to new replies.