Pester PowerShell测试提示多次输入输入

时间:2016-08-15 12:53:58

标签: powershell pester

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

我的.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

2 个答案:

答案 0 :(得分:2)

标记参数Mandatory时,必须为其提供输入值 - 否则会提示您输入一个。

来自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.

将测试更改为:

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

答案 1 :(得分:1)

如果你像这样更新你的param块

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

如果没有提示输入,您的测试将按预期失败。