Search code examples
powershellpester

Pester "Should -Invoke does not take pipeline input or ActualValue"


I am using the following test code:

Describe "Install-Programs"{
    Context "Install-Programs"{
        BeforeAll{
            Mock Start-Process {}
        }
        It "Tests"{
            My-CustomFunction #Should invoke Start-Process
            Should -Invoke Start-Process -Times 1 -Exactly
        }
    }
}

And it gives me the following error: RuntimeException: Should -Invoke does not take pipeline input or ActualValue

The call to Start-Process in My-CustomFunction does not get any pipeline input and does not get piped into any pipeline:

function My-CustomFunction(){
    ...
    (New-Object System.Net.WebClient).DownloadFile($URL, "$($Env:TEMP)\$Index.exe")
    Start-Process -FilePath "$($Env:TEMP)\$Index.exe" -ArgumentList "/S"
    ...
}

Solution

  • I don't get the error you're seeing with the example you've given. I can see that the error is actually indicating that Should -Invoke is not being used correctly, and it seems that it thinks it is being provided pipeline input or a value to the ActualValue parameter of Should. However using the example you gave (with a small edit so that the downloadfile line didn't error due to a missing input) works fine for me:

    Describe "Install-Programs" {
        
        BeforeAll {
            function My-CustomFunction() {
                $URL = 'https://www.google.com'
               (New-Object System.Net.WebClient).DownloadFile($URL, "$($Env:TEMP)\$Index.exe")
                Start-Process -FilePath "$($Env:TEMP)\$Index.exe" -ArgumentList "/S"
            }
        }
    
        Context "Install-Programs" {
            BeforeAll {
                Mock Start-Process {}
            }
            It "Tests" {
                My-CustomFunction
                Should -Invoke Start-Process -Times 1 -Exactly
            }
        }
    }
    

    I suggest double checking how/where you're using Should -Invoke to ensure its not receiving some input that you didn't intend.

    It might be worth having My-CustomFunction returning any value it outputs to a variable. I.e change your test to to this:

    It "Tests"{
        $MyResult = My-CustomFunction #Should invoke Start-Process
        Should -Invoke Start-Process -Times 1 -Exactly
    }
    

    It may be your function is returning a value to standard out that is then getting picked up by the Should.

    Alternatively it could be a bug in the Pester version you are using. I'm using 5.3.1. Check your Pester version via Get-Module Pester -ListAvailable.