Dotnet Dotnet - 1 year ago 108
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]

if($ReversString -eq $param)
return $true
return $false

And this is my

$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


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

Answer Source

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 = ''
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download