简体   繁体   English

如何检查mockobject的方法只是用一个特定的参数调用?

[英]How to check that method of a mockobject was not called only with a specific parameter?

I have a PHPUnit_Framework_MockObject_MockObject of a Logger . 我有一个LoggerPHPUnit_Framework_MockObject_MockObject

In a unit test, I do not want a call to the warn method to happen with a specific string parameter doNotCallMeWithThisString . 在单元测试中,我不希望使用特定的字符串参数doNotCallMeWithThisString调用warn方法。

I have come this far: 我走到这一步:

public function testThis()
{
    ...

    $logger = $this->getMockLogger();
    $logger->expects($this->exactly(0))->method('warn')->with(
        $this->equalTo('doNotCallMeWithThisString')
    );
    ...
}

Yet this fails because exactly(0) marks any call to the warn method as an error even is the string parameter is somethingEntirelyUnrelated . 然而这失败了因为exactly(0)将对warn方法的任何调用标记为错误,即使字符串参数是完全somethingEntirelyUnrelated

How can I tell the mock object that any call to warn is fine unless it is called with that specific string? 除非用特定的字符串调用,否则我怎么能告诉模拟对象任何warn调用都没有问题?

the exactly() method is for asserting how many times an mocked method will be called. exactly()方法用于断言模拟方法的调用次数。 Unless you are using $this->at() for the mocked behavior, you don't specify the arguments for a specific call. 除非您对模拟行为使用$this->at() ,否则不指定特定调用的参数。 exactly(0) says that the number of calls should be 0. exactly(0)表示调用次数应为0。

Change your mock to this: 将模拟更改为:

 $logger->expects($this->any()) //Or however many times it should be called
        ->method('warn')
        ->with(
              $this->callback(function($argument) {
                  return $argument !== 'doNotCallMeWithThisString';
              })
         )
 );

This uses a callback to check the argument passed to the mocked method and validates that it isn't equal to your string. 这使用回调来检查传递给模拟方法的参数,并验证它不等于您的字符串。

The PHPUnit documentation has the constraint types that you can use to verify the arguments of a mock. PHPUnit文档具有可用于验证模拟参数的约束类型。 At this time, it doesn't have a string not equals type. 此时,它没有字符串不等于类型。 But using the callback, you can make your own. 但是使用回调,你可以制作自己的回调。 Your callback just needs to check the argument used and returns true if it is ok and false if it isn't. 你的回调只需要检查使用的参数,如果可以,则返回true否则返回false

You can use the callback assertion. 您可以使用callback断言。 With this, a callback method provided by you will be invoked for each time the parameter is called: 这样,每次调用参数时都会调用您提供的回调方法:

$logger
    ->expects($this->any())
    ->method('warn')
    ->with($this->callback(function($string) {
        return $string !== 'doNotCallMeWithThisString';
    }));

Some important points about this: 关于此的一些重要观点:

  1. Since you don't actually care how often the method is called (or if it's called at all) as long as it's not called with the bad parameter, you need to use $this->any() as invocation count matcher. 因为你实际上并不关心调用该方法的频率 (或者它是否被调用),只要它没有使用bad参数调用,你需要使用$this->any()作为调用计数匹配器。
  2. The callback constraint will be invoked for each invocation of the mocked method. 将为模拟方法的每次调用调用回调约束。 It has to return TRUE for the constraint to match. 必须返回TRUE才能使约束匹配。

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

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