由于局部变量变量错误无法通过Pester Test
Can't Pass Pester Test because of local variable variable error
我很难使用 invoke-command 并在脚本块上使用 $using 变量来为特定的 Powershell 函数创建一个 pester。每当我 运行 我的测试时,它总是 return 一个错误。示例函数和测试如下:
函数:
Function Execute-Function {
.
.
.
$Name = "Computer_Name"
$ScriptBlock = {
Import-Module "Activedirectory"
Get-Computer -Name $Using:Name
}
Invoke-Command -Session $Session -ScriptBlock $ScriptBlock
}
测试:
Describe 'Execute-Function' {
.
.
.
.
mock Import-Module {} -verifiable
mock Get-Computer {} -verifiable
mock Invoke-Command {
param($Scriptblock)
. $Scriptblock
} -verifiable
$result = Execute-Function
it 'should call all verifiable mocks'{
Assert-verifiablemocks
}
}
我的测试错误 return 无法检索正在使用的变量。 using 变量只能与 Invoke-Command 一起使用.... 即使我将 Get-Computer 模拟为 return 什么也没有,我还是无法理解这个错误?还是我需要更改模拟 Get-Computer 的方式才能让我的测试通过?
提前致谢
我不确定您是否可以使用 Pester 模拟 $using:
作用域。但是,您可以利用前 $using:
范围的处理方式:
Invoke-Command -ScriptBlock {
Param(
[Parameter(Position = 0)]
[String] $Name
)
<# ... #>
} -ArgumentList $Name
我很难使用 invoke-command 并在脚本块上使用 $using 变量来为特定的 Powershell 函数创建一个 pester。每当我 运行 我的测试时,它总是 return 一个错误。示例函数和测试如下:
函数:
Function Execute-Function {
.
.
.
$Name = "Computer_Name"
$ScriptBlock = {
Import-Module "Activedirectory"
Get-Computer -Name $Using:Name
}
Invoke-Command -Session $Session -ScriptBlock $ScriptBlock
}
测试:
Describe 'Execute-Function' {
.
.
.
.
mock Import-Module {} -verifiable
mock Get-Computer {} -verifiable
mock Invoke-Command {
param($Scriptblock)
. $Scriptblock
} -verifiable
$result = Execute-Function
it 'should call all verifiable mocks'{
Assert-verifiablemocks
}
}
我的测试错误 return 无法检索正在使用的变量。 using 变量只能与 Invoke-Command 一起使用.... 即使我将 Get-Computer 模拟为 return 什么也没有,我还是无法理解这个错误?还是我需要更改模拟 Get-Computer 的方式才能让我的测试通过?
提前致谢
我不确定您是否可以使用 Pester 模拟 $using:
作用域。但是,您可以利用前 $using:
范围的处理方式:
Invoke-Command -ScriptBlock {
Param(
[Parameter(Position = 0)]
[String] $Name
)
<# ... #>
} -ArgumentList $Name