Pester v5.1:应该 - 包含方括号的消息抛出失败

Pester v5.1: Should -Throw with message containing square-brackets fails

我正在使用 pester 来测试我抛出的异常(负面测试)。

尽管看起来抛出的异常消息与我的 Should -Throw 完全匹配,但当异常消息中有方括号时,Pester 宣布测试失败。通过从抛出的异常消息中删除方括号(或用其他类型的括号替换它们),Pester 测试将满足。

以下 Pester 测试说明了这些情况:

Describe "Testing Pester Should Throw" {
   Context "Positive tests: different brackets and one without" {
      It "Test 1" {
         [String]$strMyexMsg = "my text [with square brackets] 1"
         { throw $strMyexMsg } | Should -Throw $strMyexMsg
      }
      It "Test 2" {
         [String]$strMyexMsg = "my text with * asterix"
         { throw $strMyexMsg } | Should -Throw $strMyexMsg
      }
      It "Test 3" {
         [String]$strMyexMsg = "my text (with basic brackets)" 
         { throw $strMyexMsg } | Should -Throw $strMyexMsg
      }
      It "Test 4" {
         [String]$strMyexMsg = "my text {with curly brackets}"
         { throw $strMyexMsg } | Should -Throw $strMyexMsg
      }
      It "Test 5" {
         [String]$strMyexMsg = "my text without brackets"
         { throw $strMyexMsg } | Should -Throw $strMyexMsg
      }
   }
}

我的问题:为什么测试 1 失败而测试 2...5 成功?

这是我得到的 Pester 输出:

PS D:\srcGitHpg\hpg-tools\PowerShell> Invoke-Pester .\TestPesterShouldThrow.Test.ps1

Starting discovery in 1 files.
Discovery finished in 163ms.
[-] Testing Pester Should Throw.Positive tests: different brackets and one without.Test 1 13ms (12ms|1ms)
 Expected an exception, with message 'my text [with square brackets] 1' to be thrown, but the message was 'my text [with square brackets] 1'. from D:\srcGitHpg\hpg-tools\PowerShell\TestPesterShouldThrow.Test.ps1:7 char:12
     +          { throw $strMyexMsg } | Should -Throw $strMyexMsg
     +            ~~~~~~~~~~~~~~~~~
 at { throw $strMyexMsg } | Should -Throw $strMyexMsg, D:\srcGitHpg\hpg-tools\PowerShell\TestPesterShouldThrow.Test.ps1:7
 at <ScriptBlock>, D:\srcGitHpg\hpg-tools\PowerShell\TestPesterShouldThrow.Test.ps1:7
Tests completed in 389ms
Tests Passed: 4, Failed: 1, Skipped: 0 NotRun: 0

PowerShell 版本:

PS D:\srcGitHpg\hpg-tools\PowerShell> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17763  1490

纠缠版本:

PS D:\srcGitHpg\hpg-tools\PowerShell> Get-Module -Name "Pester"

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     5.1.0      Pester                              {Add-ShouldOperator, AfterAll, AfterEach, Assert-MockCalled...}

我在 Windows 上使用 Windows 命令行,版本为:

Microsoft Windows [Version 10.0.17763.1577]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Windows\System32>

感谢您的帮助, 托米

在 Pester v5 升级期间,Should -Throw 被更改为使用 -like 来匹配异常消息,而不是像以前在 Pester v4 中那样使用 .Contains

来自Breaking changes in v5

Should -Throw is using -like to match the exception message instead of .Contains.
Use * or any of the other -like wildcard to match only part of the message.

您可以通过使用反引号 ` 转义 [] 来解决此问题;如本例所示:

Import-Module Pester -MinimumVersion '5.0.0'

function Test-Throw {
    throw "I should throw a [message]"
}

Describe 'PesterThrowBug' {
    It 'Should Throw a Message' {
        { Test-Throw } | Should -Throw -ExpectedMessage 'I should throw a `[message`]'
    }
}

这是一个相关的问题 GitHub Issue https://github.com/pester/Pester/issues/1793