tho*_*hom 40 php tdd phpunit unit-testing mocking
当我创建一个新的模拟时,我需要调用expected方法.到底是做什么的?它的论点怎么样?
$todoListMock = $this->getMock('\Model\Todo_List');
$todoListMock->expects($this->any())
->method('getItems')
->will($this->returnValue(array($itemMock)));
Run Code Online (Sandbox Code Playgroud)
我找不到任何原因(我试过docs).我已经阅读了这些消息来源,但我无法理解.谢谢.
Gor*_*don 50
查看源代码将告诉您:
/**
* 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);
Run Code Online (Sandbox Code Playgroud)
PHPUnit手册列出了可用的Matchers
- any()返回一个匹配器,当匹配它的方法被执行零次或多次时匹配.
- never()返回一个匹配器,当匹配它的方法从未执行时匹配.
- atLeastOnce()返回一个匹配器,该匹配器在评估它的方法至少执行一次时匹配.
- once()返回一个匹配器,当匹配它的方法只执行一次时匹配.
- exact(int $ count)返回一个匹配器,当匹配它的方法执行完全$ count次时匹配.
- at(int $ index)返回一个匹配器,该匹配器在给定的$ index处调用它所评估的方法时匹配.
Mar*_*era 50
expected() - 设置您希望调用方法的次数:
$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'));
Run Code Online (Sandbox Code Playgroud)
如果你知道,在expect()中使用$ this-> once()一次调用该方法,否则使用$ this-> any()
请参阅:
具有多个expected()调用的PHPUnit mock
https://phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubs
http://www.slideshare.net/mjlivelyjr/advanced-phpunit-testing
| 归档时间: |
|
| 查看次数: |
33988 次 |
| 最近记录: |