Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing for scripts with mandatory parameter prompts #237

Closed
andreasbalzer opened this issue Oct 18, 2022 · 2 comments
Closed

Testing for scripts with mandatory parameter prompts #237

andreasbalzer opened this issue Oct 18, 2022 · 2 comments

Comments

@andreasbalzer
Copy link

andreasbalzer commented Oct 18, 2022

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
Copy link
Collaborator

fflaten commented Oct 18, 2022

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

@fflaten
Copy link
Collaborator

fflaten commented Mar 27, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants