2
我是單元測試新手,並且在理解phpunit中模擬對象時遇到問題。 我有以下功能:PHPUnit模擬方法調用0次,而應該調用一次
public function createPaymentStatement()
{
$tModel = new \FspInvoice\Model\Transaction();
$paymentsArr = $this->transactionGateway->getTransactionWithStatus($tModel::SETTLED);
$result = false;
if(is_array($paymentsArr)){
//some code here
$result = $psArr;
}
return $result;
}
而現在的單向測試上面的功能:
public function testCreatePaymentStatementWithPaymentsSettledReturnsArray()
{
$this->transactionGateway = $this->getMockBuilder('FspInvoice\Model\TransactionsTable')
->setMethods(array('getTransactionWithStatus'))
->disableOriginalConstructor()
->getMock();
$this->transactionGateway->expects($this->once())
->method('getTransactionWithStatus')
->will($this->returnValue(array(0,1,2)));
$test = $this->service->createPaymentStatement();
$this->assertTrue(is_array($test));
}
但是當我運行的代碼中,我得到的錯誤:
1)FspInvoiceTest\ServiceTest\PaymentTest::testCreatePaymentStatementWithPaymentsSettledReturnsArray
Expectation failed for method name is equal to <string:getTransactionWithStatus> when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.
我究竟做錯了什麼?
是的,的確,問題是,我沒有通過嘲笑對象。謝謝 – sica07
「你應該記住的是,你不嘲笑一門課,你嘲笑一門課的對象」+1 –