Pester 没有捕获抛出的错误

2024-05-16

当我运行以下纠缠测试时,我希望它能够捕获预期的错误,但事实并非如此。但是,当我使用不同的函数和不同的 throw 语句运行测试时,它会起作用。

纠缠测试:

Describe "Remove-GenericCredential Function Tests" {
  $InternetOrNetworkAddress = 'https://[email protected] /cdn-cgi/l/email-protection'

  Context "Test:  Remove-GenericCredential -InternetOrNetworkAddress '$InternetOrNetworkAddress' (Credential does not exist)" {
  It "This Command threw an error.  The credential does not exist." { { (Remove-GenericCredential -InternetOrNetworkAddress $InternetOrNetworkAddress -Confirm:$false) } | should throw "Remove-GenericCredential : Credential $InternetOrNetworkAddress not found" }
  }
}

未捕获的错误:

Remove-GenericCredential : Credential https://[email protected] /cdn-cgi/l/email-protection not found

At C:\Users\klocke7\Documents\WindowsPowerShell\Modules\Ford_CredentialManager\Tests\Remove-GenericCredential.Tests.ps1:30 char:76
+ ... xist." { { (Remove-GenericCredential -InternetOrNetworkAddress $Inter ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Remove-GenericCredential

      [-] This Command threw an error.  The credential does not exist. 44ms
        Expected: the expression to throw an exception with message {Remove-GenericCredential : Credential https://[email protected] /cdn-cgi/l/email-protection not found}, an exception was not raised, message was {}
            from C:\Users\klocke7\Documents\WindowsPowerShell\Modules\Ford_CredentialManager\Tests\New-GitHubCredential.Tests.ps1:59 char:176
            + ... e $UserName -Token 'NotAGitHubTokenSpecialCharacters!@#$%^&*') } | sh ...
            +                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        at <ScriptBlock>, C:\Users\klocke7\Documents\WindowsPowerShell\Modules\Ford_CredentialManager\Tests\Remove-GenericCredential.Tests.ps1: line 30
        30:     It "This Command threw an error.  The credential does not exist." { { (Remove-GenericCredential -InternetOrNetworkAddress $InternetOrNetworkAddress -Confirm:$false) } | should throw 'Remove-GenericCredential : Credential https://[email protected] /cdn-cgi/l/email-protection not found' }

根据另一个答案,该函数抛出一个非终止错误 https://blogs.technet.microsoft.com/heyscriptingguy/2015/09/16/understanding-non-terminating-errors-in-powershell/因此,它不被认为符合测试Should Throw,它正在检查终止错误。

有两种方法可以解决这个问题:

  1. 您可以更改它,以便通过更改您的Write-Error to Throw.
  2. 您可以更改测试以强制函数抛出终止错误,即使发生非终止错误时也是如此-ErrorAction Stop当你调用它时(我可以看到你正在使用-Confirm,我假设你已经使用过[cmdletbinding()]在函数中添加常用参数,例如-ErrorAction).

这是第二个解决方案的示例(我模拟了顶部的函数,以便我可以测试它,但您不需要将其包含在测试脚本中):

Function Remove-GenericCredential {
    [cmdletbinding(supportsshouldprocess)]
    Param(
        $InternetOrNetworkAddress
    )

    Write-Error "Remove-GenericCredential : Credential $InternetOrNetworkAddress not found"
}

Describe "Remove-GenericCredential Function Tests" {
    $InternetOrNetworkAddress = 'https://[email protected] /cdn-cgi/l/email-protection'

    Context "Test:  Remove-GenericCredential -InternetOrNetworkAddress '$InternetOrNetworkAddress' (Credential does not exist)" {
    It "This Command threw an error.  The credential does not exist." { 
        { (Remove-GenericCredential -InternetOrNetworkAddress $InternetOrNetworkAddress -Confirm:$false -ErrorAction Stop) } | should throw "Remove-GenericCredential : Credential $InternetOrNetworkAddress not found" }
    }
}

From help Write-Error:

Write-Error cmdlet 声明一个非终止错误。默认情况下, 错误通过错误流发送到主机程序 显示,以及输出。

要写入非终止错误,请输入错误消息字符串、ErrorRecord 对象或 Exception 对象。使用 Write-Error 的其他参数来填充错误记录。

非终止错误将错误写入错误流,但它们不会停止命令处理。如果输入项集合中的一项声明非终止错误,则该命令将继续处理集合中的其他项。

要声明终止错误,请使用 Throw 关键字。更多信息请参见about_Throw (http://go.microsoft.com/fwlink/?LinkID=145153 http://go.microsoft.com/fwlink/?LinkID=145153).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Pester 没有捕获抛出的错误 的相关文章

随机推荐

  • 检索 geodjango 多边形对象的边界框

    如何在 geodjango 中获取 MultiPolygon 对象的边界框 在 API 中找不到任何内容http geodjango org docs geos html http geodjango org docs geos html
  • 检测IE8兼容模式[重复]

    这个问题在这里已经有答案了 可能的重复 区分IE7浏览器和IE7兼容模式浏览器 https stackoverflow com questions 10213639 differentiate ie7 browser and browser
  • 为现有模块创建 d.ts 文件

    我正在尝试创建一个d ts文件为React 静态容器 https github com reactjs react static container图书馆 NPM 中安装的库如下所示 var React require react var
  • 如何在 Elastic Beanstalk 上添加 PATH

    我想将 PATH 添加到包上eb deploy 软件包安装到 var www html vendor bin 可以通过SSH手动添加 但是如何使用配置文件添加PATH 我有这样的配置文件 ebextensions ec2 config 01
  • 使用seaborn绘制简单线图

    我正在尝试使用seaborn python 绘制ROC曲线 对于 matplotlib 我只需使用该函数plot plt plot one minus specificity sensitivity bs where one minus s
  • 如何在 AutoFac 上使用内部构造函数解析公共类

    我有这个类要在单元测试中实例化 public class Customer internal Customer Guid id initialize property 如果我使用另一个 单元测试 程序集实例化测试类new Customer
  • 中有样式表 吗?

    在内部链接 CSS 文件是一个坏主意吗 body 我读过 如果浏览器在外部找到另一个 CSS 文件 则它会被迫重新开始 CSS 渲染 head 只是因为它可能需要将样式应用于已经渲染的元素 另外 我认为 HTML 无法正确验证 我需要确认这
  • 以 2 为底的矩阵对数

    Logm 取矩阵对数 并且log2 取矩阵每个元素以 2 为底的对数 我正在尝试计算冯 诺依曼熵 它涉及以 2 为底的矩阵对数 我该怎么做呢 如果将 以 2 为底 的矩阵指数定义为B expm log 2 A 或者如果您类似地通过特征分解直
  • 检测 TextBox 中的 Tab 键按下

    I am trying to detect the Tab key press in a TextBox I know that the Tab key does not trigger the KeyDown KeyUp or the K
  • 为什么 std::function 不是有效的模板参数,而函数指针却是?

    我已经定义了名为的类模板CallBackAtInit其唯一目的是在初始化时调用函数 构造函数 该函数在模板参数中指定 问题是模板不接受std function作为参数 但它们接受函数指针 为什么 这是我的代码 include
  • D3 向对象添加超链接?

    我正在尝试制作 D3 图 它将代表我网站的菜单 我尝试按照此处的其他指南添加超链接 但它们都不起作用 每个对象都会有一个不同的 URL 指向 主页 关于 联系方式等 如果添加超链接 我可以拖动对象吗 这意味着如果我按住单击 如果我单击该对象
  • ASP.NET MVC Server.MapPath 在 Internet Explorer 中提供完整路径

    我正在上传文件 var filename Server MapPath Path Combine Content UserContent Path ChangeExtension newName Path GetExtension atta
  • 我需要安装什么才能获取 Microsoft.TeamFoundation.WorkItemTracking.Client.dll?

    我只需要安装VS2010 SDK http www microsoft com downloads en details aspx FamilyID 47305cf4 2bea 43c0 91cd 1b853602dcc5 是否有 TFS2
  • 这个套利机器人真的能正常运行吗?

    我一直在研究创建某种形式的加密货币套利交易机器人 最近看到了以下视频 该视频展示了一个套利机器人 该机器人通过部署到币安智能链的智能合约上的单个函数调用来运行 我已经查看了合约的代码 但不确定它是否能真正发挥作用 或者它是否是一个骗局合约
  • 无法获取本地或参数的值,因为它在此指令指针处不可用,可能是因为它已被优化掉

    Visual Studio 2010 会删除 没有其他词 不安全块中函数参数之一中的数据 什么可能导致此错误 调试器显示以下消息 Cannot obtain value of local or argument as it is not a
  • 如何增加ofstream的缓冲区大小

    我想增加 C 程序的缓冲区大小 以便它不会过于频繁地写入 默认缓冲区是 8192 字节 我尝试使用 pubsetbuf 将其增加到 200K 原始代码 ofstream fq fastq1 cstr ios out fastq1 is a
  • pygame:使用 sprite.RenderPlain 绘制精灵组的顺序

    我有一个精灵组 需要按一定的顺序绘制 以便其精灵按应有的方式重叠 然而 即使使用运算符模块函数 sorted self sprites key attrgetter y x 对组进行排序 顺序也是错误的 我该如何解决这个问题 直截了当地说
  • “没有合适的默认构造函数可用”——为什么会调用默认构造函数?

    我已经查看了与此相关的其他一些问题 但我不明白为什么在我的情况下甚至应该调用默认构造函数 我可以只提供一个默认构造函数 但我想了解它为什么这样做以及它会产生什么影响 error C2512 CubeGeometry no appropria
  • 制作已准备好开发人员 ID 的 macOS 安装程序包

    注意 这是为了OS X 安装程序 https en wikipedia org wiki Installer macOS 仅包 提交到的包Mac 应用商店 https en wikipedia org wiki Mac App Store遵
  • Pester 没有捕获抛出的错误

    当我运行以下纠缠测试时 我希望它能够捕获预期的错误 但事实并非如此 但是 当我使用不同的函数和不同的 throw 语句运行测试时 它会起作用 纠缠测试 Describe Remove GenericCredential Function T