小编jge*_*sky的帖子

Pester 不是模拟点源的函数

我正在使用 Pester 测试一个 PowerShell 脚本,该脚本点源另一个脚本。当我尝试模拟点源函数时,Pester 拒绝使用模拟版本。当我尝试通过将函数添加到 .psm1 文件并使用 Import-Module 而不是点源来获取函数时,我遇到了同样的问题。

这是一个复制我遇到的问题的示例。所有 3 个文件都在同一个文件夹中。

文件.ps1

Function Invoke-Foo{
    'Cantelope'
}
Run Code Online (Sandbox Code Playgroud)

酒吧.ps1

function Invoke-Bar {
    . .\foo.ps1
    Invoke-foo
}
Run Code Online (Sandbox Code Playgroud)

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'
    }
}
Run Code Online (Sandbox Code Playgroud)

在模拟 Invoke-Foo 之后,结果应该是 'Banana',但结果是:

Describing Bar
 [-] Mocks Foo 36ms
   Expected string length 6 but was 9. Strings differ …
Run Code Online (Sandbox Code Playgroud)

powershell unit-testing pester

4
推荐指数
1
解决办法
671
查看次数

标签 统计

pester ×1

powershell ×1

unit-testing ×1