php PHPUnit:期望方法的含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7432458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:39:28  来源:igfitidea点击:

PHPUnit: expects method meaning

phpunit-testingtddmockingphpunit

提问by thom

When I create a new mock I need to call the expects method. What exactly it does? What about its arguments?

当我创建一个新的模拟时,我需要调用 expects 方法。它究竟有什么作用?它的论据呢?

$todoListMock = $this->getMock('\Model\Todo_List');
        $todoListMock->expects($this->any())
            ->method('getItems')
            ->will($this->returnValue(array($itemMock)));

I can't find the reason anywhere (I've tried docs). I've read the sources but I can't understand it. Thanks.

我在任何地方都找不到原因(我已经尝试过文档)。我已经阅读了来源,但我无法理解。谢谢。

回答by Marek Sebera

expects() - Sets how many times you expect a method to be called:

expects() - 设置您希望方法被调用的次数:

$mock = $this->getMock('nameOfTheClass', array('firstMethod','secondMethod','thirdMethod'));
$mock->expects($this->once())
     ->method('firstMethod')
     ->will($this->returnValue('value'));
$mock->expects($this->once())
     ->method('secondMethod')
     ->will($this->returnValue('value'));
$mock->expects($this->once())
     ->method('thirdMethod')
     ->will($this->returnValue('value'));

If you know, that method is called once use $this->once() in expects(), otherwise use $this->any()

如果你知道,该方法被调用一次,在 expects() 中使用 $this->once(),否则使用 $this->any()

see:
PHPUnit mock with multiple expects() calls

请参阅:
带有多个 expects() 调用的 PHPUnit 模拟

https://phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubs

https://phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubs

http://www.slideshare.net/mjlivelyjr/advanced-phpunit-testing

http://www.slideshare.net/mjlivelyjr/advanced-phpunit-testing

回答by Gordon

A look into the source code will tell you:

查看源代码会告诉你:

/**
 * Registers a new expectation in the mock object and returns the match
 * object which can be infused with further details.
 *
 * @param  PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
 */
public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher);

And the PHPUnit Manual lists the available Matchers at

PHPUnit 手册在以下位置列出了可用的匹配器

  • any()returns a matcher that matches when the method it is evaluated for is executed zero or more times.
  • never()returns a matcher that matches when the method it is evaluated for is never executed.
  • atLeastOnce()returns a matcher that matches when the method it is evaluated for is executed at least once.
  • once()returns a matcher that matches when the method it is evaluated for is executed exactly once.
  • exactly(int $count)returns a matcher that matches when the method it is evaluated for is executed exactly $count times.
  • at(int $index)returns a matcher that matches when the method it is evaluated for is invoked at the given $index.
  • any()返回一个匹配器,当它被评估的方法被执行零次或多次时匹配。
  • never()返回一个匹配器,该匹配器在评估它的方法从未执行时匹配。
  • atLeastOnce()返回一个匹配器,当它被评估的方法至少执行一次时匹配。
  • once()返回一个匹配器,当它被评估的方法只执行一次时匹配。
  • exact(int $count)返回一个匹配器,当它被评估的方法执行精确 $count 次时匹配。
  • at(int $index)返回一个匹配器,当它被评估的方法在给定的 $index 处被调用时匹配。