简体   繁体   English

纠缠“应该 - 调用不接受管道输入或 ActualValue”

[英]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 不接受管道输入或 ActualValue

The call to Start-Process in My-CustomFunction does not get any pipeline input and does not get piped into any pipeline:My-CustomFunction中对Start-Process的调用不会获得任何管道输入,也不会通过管道传输到任何管道中:

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

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 .我可以看到错误实际上表明Should -Invoke没有被正确使用,并且它似乎认为它正在提供管道输入或ShouldActualValue参数的值。 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:但是,使用您提供的示例(进行少量编辑,以便downloadfile行不会因缺少输入而出错)对我来说效果很好:

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.我建议仔细检查你如何/在哪里使用Should -Invoke以确保它没有收到你不想要的一些输入。

It might be worth having My-CustomFunction returning any value it outputs to a variable.My-CustomFunction将它输出的任何值返回给变量可能是值得的。 Ie 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 .可能是您的 function 正在将一个值返回到标准输出,然后由Should获取。

Alternatively it could be a bug in the Pester version you are using.或者,它可能是您使用的 Pester 版本中的错误。 I'm using 5.3.1.我正在使用 5.3.1。 Check your Pester version via Get-Module Pester -ListAvailable .通过Get-Module Pester -ListAvailable检查您的 Pester 版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM