powershell - Null List in Pester Mock created from a closure -
why following code:
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) result in error:
runtimeexception: cannot call method on null-valued expression. @ <scriptblock>, <no file>: line 7 @ <scriptblock>, c:\users\notme\documents\windowspowershell\modules\pester\functions\mock.ps1: line 1018 @ executeblock, c:\users\notme\documents\windowspowershell\modules\pester\functions\mock.ps1: line 1022 @ invoke-mock, c:\users\notme\documents\windowspowershell\modules\pester\functions\mock.ps1: line 868 @ <scriptblock><process>, <no file>: line 53 for reference tracing output gives:
wwwww=>system.collections.generic.list`1[system.string] xxxxx=>c:\dummy\command.exe -s "blah" -d "blahblah" -i "something.sql" yyyyy=> when use following, works:
$dummymock = (createexecutedcommandslogger $executedcommands) &$dummymock "blah" i'm assuming way script block in mock executed?
as can see source (get-command mock).scriptblock, there single line $mockwith parameter used:
$mockwithcopy = [scriptblock]::create($mockwith.tostring()) so that, $mockwith string , closure ignored. done because on next line newly created script block $mockwithcopy bounded session state, loose closure anyway:
set-scriptblockscope -scriptblock $mockwithcopy -sessionstate $contextinfo.session pester first private copy of script block not affect passed instance.
Comments
Post a Comment