4

有人可以告诉我用 Pester 测试不可为空的字符串参数的最佳方法吗?

ParameterBindingValidationException当我将一个空字符串传递给我的 PowerShell 模块时,我收到一个。

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

1 回答 1

3

将检查放入脚本块中:

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

纠缠维基

于 2015-12-18T20:44:32.120 回答