这是与this相同的问题和解决方案,但是有一些简单的例子,希望更容易找到,因为我花了几个小时才找到上面的问题。
我有以下代码要测试:
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中使用确切的错误消息,并且使用写错误而不是抛出。这些都不适合我
发布于 2022-04-23 17:01:37
在测试Should -Throw时,代码必须位于脚本块内,因此应该如下所示:
It "Testing"{
{Test-Function -Bad "Params"} | Should -Throw
}https://stackoverflow.com/questions/71981844
复制相似问题