示例脚本的 Pester PowerShell 测试未按预期工作

Pester PowerShell testing for a sample script is not working as expected

大家好,我写了一个示例脚本来查找给定的字符串是否为回文,如下所示

function Palindrome1([string] $param)
{
  [string] $ReversString
  $StringLength = @()

  $StringLength = $param.Length

  while ( $StringLength -ge 0 )
 {
    $ReversString = $ReversString + $param[$StringLength]
    $StringLength--
 }    

 if($ReversString -eq $param)
 {
    return $true
 }
else
{
    return $false
}
}

这是我的 .tests.ps1

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here$sut"

Describe "Palindrome1" {
    It "does something useful" {
        Palindrome1 "radar" | Should Be $true
    }
}

下面是调用脚本

$modulePath = "D:\Pester-master\Pester.psm1"
$SourceDir = "E:\Pester"
Import-Module $modulePath -ErrorAction Inquire
$outputFile = Join-Path $SourceDir "TEST-pester.xml"

$result = Invoke-Pester -Path $SourceDir -CodeCoverage "$SourceDir\*.ps1" -PassThru -OutputFile $outputFile

$result

我没有得到预期的结果,谁能告诉我哪里做错了

这条语句:

[string] $ReversString

是一个导致空字符串的值表达式。每次 运行 时,Palindrome1 函数都会输出该空字符串。将其更改为:

[string] $ReversString = ''