3

我很难使用调用命令为特定的 Powershell 函数创建一个纠缠器,并且在脚本块上有一个 $using 变量。每当我运行我的测试时,它总是会返回一个错误。示例功能和测试如下:

功能:

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
}
}

我的测试错误将返回A 无法检索使用变量。using 变量只能与 Invoke-Command 一起使用....即使我嘲笑 Get-Computer 不返回任何内容,我也无法理解此错误?还是我需要更改模拟 Get-Computer 的方式才能通过测试?

先感谢您

4

1 回答 1

2

我不确定您是否可以$using:使用 Pester 来模拟范围。但是,您可以利用 pre $using:-scope 做事方式:

Invoke-Command -ScriptBlock {
    Param(
        [Parameter(Position = 0)]
        [String] $Name
    )

    <# ... #>

} -ArgumentList $Name
于 2018-01-10T22:27:50.263 回答