【问题标题】:Why PHPSpec is stepping into mocked/stubed method?为什么 PHPSpec 正在进入模拟/存根方法?
【发布时间】: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 的调用时,测试通过了。我真的不明白为什么会这样。

【问题讨论】:

    标签: php phpspec


    【解决方案1】:

    你调用 $taskListDTO->getId() 两次,但只存根一次。

    我会将你的 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){
            $taskList = $this->taskListRepository->find($taskListDTO->getId());
    
            $task = $this->taskManager->createNew()
                ->setTitle($taskDTO->getTitle())
                ->setTaskList($taskList);
    
            $addTask = $this->addTaskManager->createNew($this->taskRepository, $this->taskListRepository,$this->userRepository);
    
            $newTaskDTO = $this->taskDTOManager->createNew($task);
    
            try {
                $addTask->toTaskList($newTaskDTO, $taskListDTO);
            } catch (\ErrorException $e) {}
    
            return $newTaskDTO;
    
        }else {
            if ($task->getTitle() != $taskDTO->getTitle()) {
                $task->setTitle($taskDTO->getTitle());
            }
            $this->taskRepository->update();
        }
    
        return $this->taskDTOManager->createNew($task);;
    }
    

    这利用了 DI,您可以存根/模拟调用。

    当您指定它时,您可能会注意到您需要模拟和存根很多事情,这通常暗示该方法正在做太多事情,将一些任务委托给其他类可能是个好主意。

    【讨论】:

    • $taskListDTO->getId() 在我正在测试的方法中被调用一次。第二个是我试图排除的方法。我什至尝试添加另一个 ->willReturn(1) 但它没有用。也许你的意思是 $taskDTO->getGoogleId?我已将代码更改为仅调用一次,但测试仍未通过。
    • 你不能模拟$addTask,因为你在fromGoogleToApp中实例化它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    相关资源
    最近更新 更多