为什么下面的代码:
function CreateExecutedCommandsLogger(){
param(
[System.Collections.Generic.List[System.String]]$cmdLog
)
"WWWWW=>{0}" -f $cmdLog | Write-Host
return{
param(
[parameter(valuefrompipeline)]$command
)
"XXXXX=>{0}" -f $command | Write-Host
"YYYYY=>{0}" -f $cmdLog | Write-Host
$cmdLog.Add($command)
}.GetNewClosure()
}
$executedCommands = New-Object System.Collections.Generic.List[System.String]
Mock ExecuteSqlCommand (CreateExecutedCommandsLogger $executedCommands)
导致错误:
RuntimeException: You cannot call a method on a null-valued expression.
at <ScriptBlock>, <No file>: line 7
at <ScriptBlock>, C:\Users\notme\Documents\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1: line 1018
at ExecuteBlock, C:\Users\notme\Documents\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1: line 1022
at Invoke-Mock, C:\Users\notme\Documents\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1: line 868
at <ScriptBlock><Process>, <No file>: line 53
作为参考,跟踪输出给出:
WWWWW=>System.Collections.Generic.List`1[System.String]
XXXXX=>C:\dummy\command.exe -S "blah" -d "blahblah" -i "something.sql"
YYYYY=>
当我使用以下内容时,它可以工作:
$dummyMock = (CreateExecutedCommandsLogger $executedCommands)
&$dummyMock "blah"
我假设这与模拟中的脚本块的执行方式有关?