纠缠与BeforeAll给了"Cannot process argument transformation on parameter 'Scriptblock'"



我在VS代码中的Powershell repo中添加了一个新的Pester 5单元测试文件。

当我尝试运行/调试它时,我会在Terminal:中得到提示

cmdlet BeforeAll at command pipeline position 1
Supply values for the following parameters:
Scriptblock:

如果我输入一个随机字符串"0";hgj";,我得到错误:

[-] Discovery in C:XXX.Tests.ps1 failed with:
System.Management.Automation.ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'Scriptblock'. Cannot convert the "hgj" value of type "System.String" to type "System.Management.Automation.ScriptBlock". ---> System.Management.Automation.ArgumentTransformationMetadataException: Cannot convert the "hgj" value of type "System.String" to type "System.Management.Automation.ScriptBlock". ---> System.Management.Automation.PSInvalidCastException: Cannot convert the "hgj" value of type "System.String" to type "System.Management.Automation.ScriptBlock".

这是单元测试文件:

BeforeAll 
{
}
Describe 'Start-Package564' 
{
It 'Runs Start-Package' 
{

}
}

如果我先评论一下,一切都会好起来的。

如果我在Before All中添加一些代码,我仍然会得到提示和错误。

为什么"全部之前"会导致终端提示输入脚本块?

BeforeAll是一个命令,而不是关键字(与DescribeIt相同(。

因此,您需要在同一行上提供scriptblock literal,以便PowerShell知道您打算将其作为参数参数传递给BeforeAll,否则它只会看到两个单独的语句:在没有任何参数的情况下调用BeforeAll,然后是scriptblock literate。

要修复:

BeforeAll {
}
Describe 'Start-Package564' {
It 'Runs Start-Package' {

}
}

最新更新