PHPUnit:期望方法含义

发布于 2024-12-04 15:24:06 字数 330 浏览 1 评论 0原文

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

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

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

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

$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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲鸠 2024-12-11 15:24:06

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'));

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

参见:
具有多个 Expects() 调用的 PHPUnit 模拟

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

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

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

$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()

see:
PHPUnit mock with multiple expects() calls

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

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

烟─花易冷 2024-12-11 15:24:06

查看源代码会告诉您:

/**
 * 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);

PHPUnit 手册在

  • any() 返回一个匹配器,当评估它的方法执行零次或多次时,该匹配器会匹配。
  • never() 返回一个匹配器,当它所评估的方法从未执行时,该匹配器会匹配。
  • atLeastOnce() 返回一个匹配器,当评估它的方法至少执行一次时,该匹配器会匹配。
  • once() 返回一个匹配器,当评估它的方法恰好执行一次时,该匹配器就会匹配。
  • exactly(int $count) 返回一个匹配器,当评估它的方法精确执行 $count 次时,该匹配器会匹配。
  • at(int $index) 返回一个匹配器,当在给定的 $index 处调用它所评估的方法时,该匹配器会匹配。

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

  • 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文