3
我正在使用Pester测试一个PowerShell脚本点源其他脚本。当我尝试模拟点源的功能时,Pester拒绝使用模拟版本。当我尝试通过将函数添加到.psm1文件并使用Import-Module而不是点源来获取函数时,我遇到了同样的问题。纠缠不点功能是点源
下面是一个复制我遇到的问题的示例。所有3个文件都在同一个文件夹中。
Foo.ps1
Function Invoke-Foo{
'Cantelope'
}
Bar.ps1
function Invoke-Bar {
. .\foo.ps1
Invoke-foo
}
Bar.tests.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"
. .\Foo.ps1
Describe "Bar" {
It "Mocks Foo" {
Mock Invoke-Foo {'Banana'}
Invoke-Bar | should be 'Banana'
}
}
嘲笑调用-富后,结果应该是 '香蕉',但结果是:
Describing Bar
[-] Mocks Foo 36ms
Expected string length 6 but was 9. Strings differ at index 0.
Expected: {Banana}
But was: {Cantelope}
-----------^
9: Invoke-Bar | should be 'Banana'
at <ScriptBlock>, C:\Users\geinosky\Desktop\PingTest\Bar.tests.ps1: line 9
如何让Pester正确使用点源功能?