如何“获取对 Pester 中的 Mock 进行调用的参数"(或以其他方式生成包含实际值和预期值的有用消息)? [英] How to 'get arguments for calls made on' a Mock in Pester (or otherwise generate a helpful message containing actual and expected values)?

查看:68
本文介绍了如何“获取对 Pester 中的 Mock 进行调用的参数"(或以其他方式生成包含实际值和预期值的有用消息)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然有许多使用 Pester Assert a Mock 的示例,但我无法找到关于如何使用 Pester 获取参数的好的(或任何)示例在模拟上;这对于获得有意义的错误消息而不是一般有用的消息很有用:

While there are many examples of using Pester to Assert a Mock, I am unable to find good (or any) examples on how to use Pester to get parameters made upon a Mock; this is useful to get a meaningful error message instead of a generic message of so-so usefulness:

预期 Invoke-XYZ 至少被调用 1 次,但被调用了 0 次

Expected Invoke-XYZ to be called at least 1 times but was called 0 times

谢谢,显然;也可能是一个不起作用"的问题.

Thanks, and obviously; might as well be a "didn't work" SO question.

例如,在 RhinoMocks + NUnit (C#) 中,您可能会使用类似于以下的代码来断言该模拟被称为 并且 参数匹配某些条件.如果进行了调用并且参数匹配,则会显示有用的断言消息:

In RhinoMocks + NUnit (C#), for example, one might use code similar to the following to assert that the mock was called and the arguments match some criteria. If the call was made and the arguments do not match, then a useful assertion message is displayed:

var actual = mock.GetArgumentsForCallsMadeOn(m => ..);
Assert.That(actual, Is.EqualTo(expected));

如何在 Pester 中完成相同(或相似)的操作?

How can the same (or similar) be done in Pester?

目标是使失败的模拟断言导致有用错误消息,关于什么特定参数未达到预期,以及什么em> 值.

The goal is to make failing mock assertions result in useful error messages as to what particular parameter failed the expectations, and with what value(s).

PowerShell 是 5.1,Pester 是 4.8.1.

PowerShell is 5.1 and Pester is 4.8.1.

推荐答案

我有同样的问题/疑问,关于 Pester 测试和参数检查;总是很难追踪哪个参数可能使给定的 -ParameterFilter 表达式失败.

I have the same issue/question about Pester testing with parameter checks; it's always been hard to track down which parameter may have failed a given -ParameterFilter expression.

它已在几个 GitHub 问题中提出:

It's been raised in a couple of GitHub issues:

  1. https://github.com/pester/Pester/issues/376
  2. https://github.com/pester/Pester/issues/1160

但似乎还没有任何决议或共识.如果可以,我会尝试添加到那里的对话中.

But it doesn't seem like there was any resolution or consensus yet. I would try to add to the conversation there if you can.

一位用户谈到制作一个在 Assert-MockCalled 调用中调用的验证函数:

One user talked about making a Verify function that is called within the Assert-MockCalled call:

It 'multiple expressions, second line' {
    Assert-MockCalled f 1 {
        ($a -eq 1 | Verify) -and
        ($b -eq 1 | Verify) -and
        ($b-1 -eq $a | Verify)
    }
}

它提供了更好的输出,但这是一种解决这个问题的笨拙/冗长的方法.

which gave better output, but was a hacky/verbose way of getting around this.

就我个人而言,我的解决方法是编写多个 Assert-MockCalled 语句,每个语句都带有一个仅检查一个参数的 -ParameterFilter 表达式,以便我可以知道哪个参数失败基于哪个 Assert-MockCalled 语句失败.但是,如果在函数内多次调用模拟,则这种方法效果不佳.

Personally, my workaround has been to write multiple Assert-MockCalled statements, each with a -ParameterFilter expression that checks only one parameter, so that I can know which parameter failed based on which Assert-MockCalled statement failed. But, this approach does not work so well if, say, the mock is called multiple times within the function.

这篇关于如何“获取对 Pester 中的 Mock 进行调用的参数"(或以其他方式生成包含实际值和预期值的有用消息)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆