如何从另一个脚本嘲笑pscustomobject的脚本?
我们正在尝试弄清楚如何使用Pester从另一个脚本嘲笑PscustomObject的脚本。
script1.ps1
$script2 = & $Script2PS1Path -programName $myScriptName
function Invoke-MyFunction {
$script2.outHost("test data")
Get-ChildItem -Directory -Path $path -Filter "ABC_*"
...
...
}
script2.ps1
param (
[Parameter(Mandatory=$True,ValueFromPipeline=$False,HelpMessage="Mandatory.")]
[string]$programName
)
$ErrorActionPreference = "Stop"
$obj = New-Object PSCustomObject
$obj | Add-Member -MemberType NoteProperty -name prog -Value $programName
$obj | Add-Member -MemberType ScriptMethod -name outHost -Value {
param (
[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
$text,
[Parameter(Mandatory=$False,ValueFromPipeline=$True)]
[string]$foregroundcolor,
[Parameter(Mandatory=$False,ValueFromPipeline=$True)]
[string]$backgroundcolor
)
if ($foregroundcolor -and $backgroundcolor) {
Write-Host -Foregroundcolor $foregroundcolor -Backgroundcolor $backgroundcolor $text
} elseif ($foregroundcolor) {
Write-Host -Foregroundcolor $foregroundcolor $text
} elseif ($backgroundcolor) {
Write-Host -Backgroundcolor $backgroundcolor $text
} else {
Write-Host $text
}
}
$obj
exit(0)
在上面的示例中,我们需要模拟$ script2.uthost
test Indoke-myfunction
。模拟$ script2.outhost
的最佳方法是什么?
We are trying to figure out how to mock a ScriptMethod of PSCustomObject from another script using Pester.
Script1.ps1
$script2 = & $Script2PS1Path -programName $myScriptName
function Invoke-MyFunction {
$script2.outHost("test data")
Get-ChildItem -Directory -Path $path -Filter "ABC_*"
...
...
}
Script2.ps1
param (
[Parameter(Mandatory=$True,ValueFromPipeline=$False,HelpMessage="Mandatory.")]
[string]$programName
)
$ErrorActionPreference = "Stop"
$obj = New-Object PSCustomObject
$obj | Add-Member -MemberType NoteProperty -name prog -Value $programName
$obj | Add-Member -MemberType ScriptMethod -name outHost -Value {
param (
[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
$text,
[Parameter(Mandatory=$False,ValueFromPipeline=$True)]
[string]$foregroundcolor,
[Parameter(Mandatory=$False,ValueFromPipeline=$True)]
[string]$backgroundcolor
)
if ($foregroundcolor -and $backgroundcolor) {
Write-Host -Foregroundcolor $foregroundcolor -Backgroundcolor $backgroundcolor $text
} elseif ($foregroundcolor) {
Write-Host -Foregroundcolor $foregroundcolor $text
} elseif ($backgroundcolor) {
Write-Host -Backgroundcolor $backgroundcolor $text
} else {
Write-Host $text
}
}
$obj
exit(0)
In the example above we need to mock $script2.outHost
to test Invoke-MyFunction
. What is the best way to mock $script2.outHost
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上取决于您如何在测试中调用它 +这确实是脚本而不是模块(增加了一些复杂性)。
如果
script1.ps1
在beforeall
-block中点缀,然后您在iT-block中运行Invoke-myfunction
,然后只需覆盖scriptMethod如评论中提到的,例如。如果
script1.ps1
确实在脚本内注入Indoke-myfunction
,因此您的测试仅执行& script1.ps1
,然后您必须模拟add-member
,并使用-parameterFilter
来定位正确的调用。例如:script1.ps1
script1.tests.ps1
输出:
It really depends on how you invoke this in your tests + that this is really scripts and not modules (adds some complexity).
If
Script1.ps1
is dot-sourced in aBeforeAll
-block and you runInvoke-MyFunction
in the It-block, then simply override the ScriptMethod as mentioned in the comments, ex.If
Script1.ps1
really invokesInvoke-MyFunction
inside the script, so your test only execute& Script1.ps1
, then you'd have to mockAdd-Member
and target the right call using-ParameterFilter
. Ex:Script1.ps1
Script1.tests.ps1
Output: