【发布时间】:2012-01-14 01:37:05
【问题描述】:
当我使用 $this->at(0) 而不是 $this->once() 时,我的测试失败了。我肯定在这里错过了一点,但我不知道是什么。有人知道那是什么吗?
/**
* Passes
*/
public function testOne()
{
$expected = array(
'id' => 1,
'name' => 'Product Name'
);
$mock = $this->getMock('WS');
$mock->expects($this->once())
->method('getProductInfo')
->with($this->equalTo(1))
->will($this->returnValue($expected));
$this->object->setWs($mock);
// same as $mock->getInfo(1)
$returned = $this->object->getWs()->getProductInfo(1);
$this->assertEquals($expected, $returned);
}
/**
* Fails
*/
public function testOne()
{
$expected = array(
'id' => 1,
'name' => 'Product Name'
);
$mock = $this->getMock('WS');
$mock->expects($this->at(0)) // all that changed
->method('getProductInfo')
->with($this->equalTo(1))
->will($this->returnValue($expected));
$this->object->setWs($mock);
// returned equals NULL
// same as $mock->getInfo(1)
$returned = $this->object->getWs()->getProductInfo(1);
$this->assertEquals($expected, $returned);
}
【问题讨论】: