【发布时间】:2013-04-18 05:12:17
【问题描述】:
下面是代码示例
<?php
interface iFS
{
public function read();
public function write($data);
}
class MyClass
{
protected $_fs = null;
public function __construct(iFS $fs)
{
$this->_fs = $fs;
}
public function run(array $list)
{
foreach ($list as $elm)
{
$this->_fs->write($elm);
}
return $this->_fs->read();
}
}
class MyTests extends PHPUnit_Framework_TestCase
{
public function testFS()
{
$mock = $this->getMock('iFS');
$mock->expects($this->at(0))
->method('read')
->will($this->returnValue('tototatatiti'));
$c = new MyClass($mock);
$result = $c->run(array('toto', 'tata', 'titi'));
$this->assertEquals('tototatatiti', $result);
}
}
这绝对不是一个真实的案例,但它使 phpunit 和 at($index) 功能发生了一些奇怪的事情。
我的问题很简单,测试失败正常吗?
我明确要求返回“tototatatiti”,但它从未发生......
当
- 我删除了 $this->_fs->write($elm); 行 或
- 我将 $mock->expects($this->at(0)) 替换为 $mock->expects($this->once())
测试通过绿色
有什么我不明白的地方吗?
编辑:
$mock->expects($this->at(3)) ->方法('读取') ->will($this->returnValue('tototatatiti'));
=> 将使测试通过绿色...
【问题讨论】:
-
看来$this->at($index) 特性不适用于指定的方法,而是适用于整个mocked对象...如果是这样的话,这完全没用!跨度>
标签: php mocking phpunit indexing