这是与此相同的问题和解决方案,但使用更简单的示例并且希望更容易找到,因为我花了几个小时才找到上述问题。
我有以下要测试的代码:
Test-Function(){
{...}
if($ExitCode -ne 0){
throw "Stopped with exit code $ExitCode"
}
}
我正在尝试使用以下方法对其进行测试:
It "Testing"{
Test-Function -Bad "Params" | Should -Throw
}
当我运行它时,我收到以下错误:
[-] Testing 1.03s (1.03s|2ms)
RuntimeException: Stopped with exit code 10
At Test-Function {...}
我已经尝试使用确切的错误消息Should -Throw
并使用 Write-Error 而不是抛出。这些都不适合我
在测试 时Should -Throw
,代码必须在脚本块内,所以它应该如下所示:
It "Testing"{
{Test-Function -Bad "Params"} | Should -Throw
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句