单元测试ParameterBindingValidationException

时间:2015-12-18 16:05:10

标签: unit-testing powershell pester

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

当我将一个空字符串传递给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
  }
}

1 个答案:

答案 0 :(得分:3)

将支票放入脚本栏中:

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

请参阅Pester Wiki