我正在升级一些非常旧的测试以与 Pester 5 一起使用。当时Should -Throw
不支持指定应该抛出的异常类型,所以我推出了自己的断言。现在我想用 替换我自制的断言Should -Throw -ExceptionType
。然而,我在网上找不到关于如何指定要使用的异常类型的信息Should -Throw -ExceptionType
。
Pester Assertions Reference-ExceptionType
页面根本没有提及。我找到的唯一参考是一篇旧博客文章,https://jakubjares.com/2017/12/19/using-should-throw/,其中包含代码:
Should -Throw -ExceptionType ([ArgumentException])
有人可以解释一下格式吗?是做什么的([...])
?如何指定非标准异常类型?我想检查一下是否有ParameterBindingValidationException
. 我试过了
Should -Throw -ExceptionType ([System.Management.Automation.ParameterBindingValidationException])
和
Should -Throw -ExceptionType ([System.Management.Automation.ParameterBindingValidationException, System.Management.Automation])
和
Should -Throw -ExceptionType ([System.Management.Automation.ParameterBindingValidationException, System.Management.Automation, Version=7.3.7.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35])
但当我运行测试时,我收到类似于以下内容的错误:
RuntimeException:无法找到类型 [System.Management.Automation.ParameterBindingValidationException]
如果我将异常类型替换为
Should -Throw -ExceptionType ([System.ArgumentException])
然后,Pester 似乎识别出了我指定的异常类型。我收到以下消息:
预期会引发类型为 [System.ArgumentException] 的异常,但异常类型为 [System.Management.Automation.ParameterBindingValidationException、System.Management.Automation、Version=7.3.7.500、Culture=neutral、PublicKeyToken=31bf3856ad364e35]。
所以我很困惑应该如何指定应该抛出的异常。