Dotnet Dotnet - 3 months ago 14
PowerShell Question

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

Hi all I have a written a sample script to find whether the given string is Palindrome or not as follows

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
}
}


And this is my
.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
}
}


Below is Invoke script

$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


I am not getting the expected result can some one tell me where I am doing wrong

Answer

This statement:

[string] $ReversString

is a value expression resulting in an empty string. That empty string is output by the Palindrome1 function every time your run it. Change it to:

[string] $ReversString = ''