pester / docs

Source files for the Pester website.
https://pester.dev
MIT License
41 stars 77 forks source link

Testing for scripts with mandatory parameter prompts #237

Closed andreasbalzer closed 1 year ago

andreasbalzer commented 2 years ago

Hi, I'm trying to test a script with mandatory parameters. Despite supplying the parameter value, PowerShell still prompts to enter the value. I'm using Pester 5.3.3. What am I doing wrong?

Test.ps1:

[CmdletBinding()]
Param (
    [Parameter( ValueFromPipeline=$true,
                ValueFromPipelineByPropertyName=$true,
                Mandatory=$true,
                HelpMessage="foo parameter")]
    [string]$foo,

    [Parameter( ValueFromPipeline=$true,
                ValueFromPipelineByPropertyName=$true,
                HelpMessage="bar parameter")]
    [switch]$bar
)
Write "Hello $foo"

Test.Tests.ps1:

BeforeAll {
    . $PSCommandPath.Replace('.Tests.ps1', '.ps1')
}

Describe "Test" {
    It "Returns expected output" {
        $Params = @{
            foo = "goodbye"
            bar   = $true
        }
        ./Test.ps1 @Params | Should -Be "YOUR_EXPECTED_VALUE"
    }
}

PowerShell output:

invoke-pester Test*

Starting discovery in 1 files.
Discovery found 1 tests in 67ms.
Running tests.

cmdlet Test.ps1 at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
foo: world
[-] Test.Returns expected output 20ms (20ms|1ms)
 Expected strings to be the same, but they were different.
 Expected length: 19
 Actual length:   13
 Strings differ at index 0.
 Expected: 'YOUR_EXPECTED_VALUE'
 But was:  'Hello goodbye'
            ^
 at ./Test.ps1 @Params | Should -Be "YOUR_EXPECTED_VALUE", D:\src\Scripts\Test.Tests.ps1:11
 at <ScriptBlock>, D:\src\Scripts\Test.Tests.ps1:11
Tests completed in 3.42s
Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0
fflaten commented 2 years ago

Remove the BeforeAll-block. You're invoking the script without parameters.

fflaten commented 1 year ago

I assume this is ready to be closed. Can U fix @bravo-kernel ? 🙂