【发布时间】:2015-06-29 07:36:37
【问题描述】:
我从 PHPSpec 开始,但遇到了一些问题。 其中之一是我不断得到
方法调用: - 查找(空) 在 Double\TaskRepositoryInterface\TaskRepositoryInterface\P95 上不是预期的,预期的调用是: - findOneByGoogleId(exact("googleId")) 我不知道为什么 phpspec 期望 findOneByGoogleId 在那里。
这是我的规范示例:
public function it_should_synchronize_new_task_from_google_to_app(
TaskDTO $taskDTO,
TaskDTO $taskDTO2,
TaskListDTO $taskListDTO,
TaskRepositoryInterface $taskRepository,
TaskListRepositoryInterface $taskListRepository,
TaskList $taskList,
AddTask $addTask
){
$taskDTO->getGoogleId()->willReturn('googleId');
$taskRepository->findOneByGoogleId('googleId')->willReturn(null);
$taskDTO->getTitle()->willReturn('GoogleTitle');
$taskListDTO->getId()->willReturn(1);
$taskListRepository->find(1)->willReturn($taskList);
//$addTask->toTaskList(Prophecy\Argument::type(TaskDTO::class),Prophecy\Argument::type(TaskListDTO::class))->shouldBeCalled();
//$addTask->toTaskList(Prophecy\Argument::type(TaskDTO::class),Prophecy\Argument::type(TaskListDTO::class))->shouldBeCalled()->willReturn($taskDTO2);
$addTask->toTaskList(Prophecy\Argument::type(TaskDTO::class),Prophecy\Argument::type(TaskListDTO::class))->willReturn($taskDTO2);
$this->fromGoogleToApp($taskDTO, $taskListDTO)->shouldReturnAnInstanceOf('Itmore\Core\Entity\TaskDTO');
}
这是SUS:
public function fromGoogleToApp(TaskDTO $taskDTO, TaskListDTO $taskListDTO)
{
if(!$taskDTO->getGoogleId()){
throw new \ErrorException('not a google task');
}
$task = $this->taskRepository->findOneByGoogleId($taskDTO->getGoogleId());
if(!$task){
$task = new Task();
$task->setTitle($taskDTO->getTitle());
$taskList = $this->taskListRepository->find($taskListDTO->getId());
$task->setTaskList($taskList);
$addTask = new AddTask($this->taskRepository, $this->taskListRepository,$this->userRepository);
try {
$addTask->toTaskList(new TaskDTO($task), $taskListDTO);
} catch (\ErrorException $e) {}
return new TaskDTO($task);
}else {
if ($task->getTitle() != $taskDTO->getTitle()) {
$task->setTitle($taskDTO->getTitle());
}
$this->taskRepository->update();
}
return new TaskDTO($task);
}
这是 PHPSpec 正在介入的方法:
public function toTaskList(TaskDTO $taskDTO, TaskListDTO $taskListDTO)
{
$taskList = $this->taskListRepository->find($taskListDTO->getId());
if (!$taskList) {
throw new \ErrorException('taskList not found');
}
$task = $this->taskRepository->find($taskDTO->getId());
if ($task) {
throw new \ErrorException('task already exists');
}
$task = new Task();
$task->setTitle($taskDTO->getTitle());
$task->setGoogleId($taskDTO->getGoogleId());
$task->setTaskList($taskList);
$this->taskRepository->add($task);
return new TaskDTO($task);
}
如您所见,我尝试了不同的方法来存根/模拟这个 toTaskList 方法,但它似乎不起作用。当我注释掉对 toTaskList 的调用时,测试通过了。我真的不明白为什么会这样。
【问题讨论】: