【发布时间】:2019-10-24 19:14:23
【问题描述】:
我正在做单元测试,我的存储库有这个方法:
/**
* @param int|Collection $id
* @return MyModel|Collection|null
*/
public function find($id);
我试过这个:
$this->package_repo = Mockery::mock(PackageRepositoryInterface::class);
$this->app->instance('Package', $this->package_repo);
// ...
$this->ticket_repo->shouldReceive("find")->with(collect([]))->andReturn(/*...*/);
//...
echo $this->ticket_repo->find(collect([])); // Failed here.
测试立即失败,我认为这是因为shouldReceive 期望语句和实际find 语句中的2 个集合是不同的对象。
如果我想模拟$this->ticket_repo->find($ids)的行为,我该怎么办?
或者更确切地说,如果有更好的方法来设计可测试的算法,它会怎样?因为我希望将所有要查找的 id 放在一个数组中,并构造成一个 SQL 选择查询,而不是几百个单独的查询,以提高性能。
【问题讨论】:
标签: php laravel phpunit mockery