我想测试一个公共方法,该方法使用方法作为参数调用来自不同类的方法。
public function __construct(LeadData $leadData,LeadRepository $leadRepository){....}
public function proceedtosave($leads,$payout,$received)
{
try {
//save the lead
$this->savedLead = $this->leadRepository->leadCreate($leads,$payout,$received);
$this->responseData['lead_id'] = $this->savedLead->id;
} catch (QueryException $exception) {
$errorCode = $exception->errorInfo[1];
Log::info(['Lead insert error code: '.$errorCode,'Error message: '.$exception->getMessage()]);
if ($this->validator->errorCodeChecker($errorCode) === false) {
$this->leadDuplicateRepository->leadDuplicateCreate($this->leads, $payout, $received);
return $this->validator->getErrors();
}
}
}
这是我编写测试的方式
/**
* @test
*
*/
public function save_leads_to_leads_table_if_not_duplicate()
{
$this->getLeadsForValidator();
$leadData = $this->getMock('App\Helpers\..\..Data');
$leadIn = $this->getMockbuilder('App\Helpers\Repositories\..Interface')
->setMethods(array('leadCreate'))
->getMock();
$leadIn->expects($this->once())
->method('leadCreate')
->will($this->returnValue(1));
$leadIn->leadCreate($this->results,1,2);
$this->SUT = new LeadStore($leadData,$leadIn);
$this->SUT->proceedtosave($this->results,1,2);
}
我在这里更新了原来的问题,因为我意识到我必须重构我的代码。您将如何通过此测试?我收到了我要解决的错误
1) StoreLeadsTest::save_leads_to_leads_table_if_not_duplicate
.....::leadCreate(Array (...), 1, 2) was not expected to be called more than once.
/home/vagrant/Code/l.../../LeadStore.php:87
/home/vagrant/Code/./../StoreLeadsTest.php:46
代码行
$this->savedLead = $this->leadRepository->leadCreate($leads,$payout,$received);//LeadStore.php:87
$this->SUT->proceedtosave($this->results,1,2);//StoreLeadsTest.php:46
从
交换Mockery的期望 ->expects($this->once())
要
->expects($this->any())
将导致ErrorException
ErrorException: Trying to get property of non-object
指向这行代码
$this->responseData['lead_id'] = $this->savedLead->id;
如果我删除上面的那行,我的测试通过,如何让phpunit跳过该行,以便我可以继续测试其他行?