简体   繁体   English

如何'在Pester模拟中调用调用的参数(或以其他方式生成包含实际值和期望值的有用消息)?

[英]How to 'get arguments for calls made on' a Mock in Pester (or otherwise generate a helpful message containing actual and expected values)?

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; 虽然有许多使用Pester 断言模拟的例子,但是我无法找到关于如何使用Pester获取模拟参数的好(或任何)示例; this is useful to get a meaningful error message instead of a generic message of so-so usefulness: 这对于获取有意义的错误消息非常有用,而不是具有如此实用性的通用消息:

Expected Invoke-XYZ to be called at least 1 times but was called 0 times 预期调用-XYZ至少被调用1次,但被调用0次

Thanks, and obviously; 谢谢,显然; might as well be a "didn't work" SO question. 也许是一个“没有用”的问题。

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. 例如,在RhinoMocks + NUnit(C#)中,可以使用类似于以下内容的代码断言调用mock 并且参数符合某些条件。 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));

How can the same (or similar) be done in Pester? 如何在Pester中完成同样的(或类似的)?

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 is 5.1 and Pester is 4.8.1. PowerShell是5.1,而Pester是4.8.1。

I have the same issue/question about Pester testing with parameter checks; 关于Pester测试和参数检查,我有同样的问题/疑问; it's always been hard to track down which parameter may have failed a given -ParameterFilter expression. 总是很难找到哪个参数可能在给定的-ParameterFilter表达式中失败。

It's been raised in a couple of GitHub issues: 它出现在几个GitHub问题中:

  1. https://github.com/pester/Pester/issues/376 https://github.com/pester/Pester/issues/376
  2. https://github.com/pester/Pester/issues/1160 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. 如果可以,我会尝试在那里添加对话。

One user talked about making a Verify function that is called within the Assert-MockCalled call: 一位用户谈到在Assert-MockCalled调用中调用Verify函数:

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. 这提供了更好的输出,但是这是一种hacky / verbose方式。

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. 就个人而言,我的解决方法是编写多个Assert-MockCalled语句,每个语句都带有一个-ParameterFilter表达式,该表达式只检查一个参数,这样我就可以根据哪个Assert-MockCalled语句失败来知道哪个参数失败了。 But, this approach does not work so well if, say, the mock is called multiple times within the function. 但是,如果在函数内多次调用mock,这种方法效果不佳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM