And*_*ree 5 php phpunit mocking
有人能为我提供一个好的PHPUnit模拟指南吗?官方文档中的那个似乎不够详细.我正在尝试通过阅读源代码来学习PHPUnit,但我不熟悉术语匹配器,调用模拟器,存根返回等.
我需要知道以下内容:
1)如何期望多次调用模拟对象的方法,但每个方法返回一组不同的值?
$tableMock->expects($this->exactly(2))
->method('find')
->will($this->returnValue(2)); // I need the second call to return different value
Run Code Online (Sandbox Code Playgroud)
2)如何期望使用多个参数调用模拟对象的方法?
Tim*_*tle 12
[ 注意:链接的站点中的所有代码示例,请按照链接进行更详尽的说明.]
(当前)PHPUnit文档建议使用回调或onConsecutiveCalls():
$stub->expects($this->any())
->method('doSomething')
->will($this->returnCallback('str_rot13'));
$stub->expects($this->any())
->method('doSomething')
->will($this->onConsecutiveCalls(2, 3, 5, 7));
Run Code Online (Sandbox Code Playgroud)
with()可能包含多个参数:
$observer->expects($this->once())
->method('reportError')
->with($this->greaterThan(0),
$this->stringContains('Something'),
$this->anything());
Run Code Online (Sandbox Code Playgroud)
虽然没有问,但是在相关主题上(而不是我能找到的PHPUnit文档中),您可以at()用来设置对方法的多次调用的期望:
$inputFile->expects($this->at(0))
->method('read')
->will($this->returnValue("3 4"));
$inputFile->expects($this->at(1))
->method('read')
->will($this->returnValue("4 6"));
$inputFile->expects($this->at(2))
->method('read')
->will($this->returnValue(NULL));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1809 次 |
| 最近记录: |