Skip to content

How to Test Function Arguments #2423

Answered by fflaten
leewilmott asked this question in Q&A
Discussion options

You must be logged in to vote

Hi. You may use the Should -Throw assertion for this by examining the error/exception.
See https://pester.dev/docs/assertions/#throw and https://pester.dev/docs/commands/Should#throw

E.g.

Describe 'Add-Ten' {
    Context "Add-Ten: Testing invalid input" {
        It 'Throws when passing non-int value to -Number' {
            # Only check for exception
            { Add-Ten -Number "string" } | Should -Throw

            # Verify specific error message (depends on OS-language)
            { Add-Ten -Number "string" } | Should -Throw -ExpectedMessage '*Cannot convert value "string" to type "System.Int32"*'

            # Verify using error id (found using $error[0].FullyQualifiedErrorId)

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@leewilmott
Comment options

@fflaten
Comment options

@leewilmott
Comment options

Answer selected by leewilmott
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants