单元测试 ParameterBindingValidationException

Unit test ParameterBindingValidationException

谁能告诉我用 Pester 测试不可为 null 的字符串参数的最佳方法?

当我向 PowerShell 模块传递一个空字符串时,我收到 ParameterBindingValidationException

function Get-MyFunc {
  param (
    [parameter(Mandatory=$true)]
    [string]$stringParameter
  )

  ## rest of function logic here
}

我原本希望能够在我的测试中做到这一点:

Describe 'When calling Get-MyFunc with empty parameters' {
  It 'Should throw an exception' {
    Get-MyFunc '' | Should Throw
  }
}

或者这样:

Describe 'When calling Get-MyFunc with empty parameters' {
  It 'Should throw an exception' {
    PesterThrow { Get-MyFunc '' } | Should Be $true
  }
}

将检查放在脚本块中:

Describe 'When calling Get-MyFunc with empty parameters' {
  It 'Should throw an exception' {
    { Get-MyFunc '' } | Should Throw
  }
}

Pester Wiki