Developer
Developer

Reputation: 8646

Pester PowerShell testing prompting to enter the input multiple times

function Palindrome1
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [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
    }
}

My .tests.ps1

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Palindrome1.ps1"

Describe -Tags "Example" "Palindrome1" {
    It "does something useful" {
        Palindrome1 | Should Be $true
    }
}

Enter image description here

Upvotes: 0

Views: 447

Answers (2)

Jonathan Walz
Jonathan Walz

Reputation: 11

If you update your param block like this

param (
    [ValidateNotNullorEmpty()]
    [string] $param = $(throw "a parameter is required")
)

Your test will fail as expected without prompting for input.

Upvotes: 1

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174760

When you mark a parameter Mandatory, you MUST supply an input value to it - otherwise it will prompt you for one.

From Get-Help about_Parameters:


 PARAMETER ATTRIBUTE TABLE
 [...] 
   Parameter Required?
     This setting indicates whether the parameter is mandatory, that
     is, whether all commands that use this cmdlet must include this
     parameter. When the value is "True" and the parameter is missing
     from the command, Windows PowerShell prompts you for a value for
     the parameter.

Change your test to:

Describe -Tags "Example" "Palindrome1" {
    It "does something useful" {
        Palindrome1 -param "value goes here" | Should Be $true
    }
}

Upvotes: 1

Related Questions