【问题标题】:Pester v5.1: Should -Throw with message containing square-brackets failsPester v5.1:应该抛出包含方括号的消息失败
【发布时间】:2021-03-21 00:05:02
【问题描述】:

我正在使用 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>

感谢您的帮助, 汤米

【问题讨论】:

  • 我不认为你的说法有什么问题。我想你偶然发现了一个真正的错误。您应该打开一张票,以便他们查看。

标签: powershell unit-testing windows-10 throw pester


【解决方案1】:

在 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`]'
    }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    相关资源
    最近更新 更多