我有一个与以下相同的界面:
interface UserRepository
{
public function save(User $user): User;
}
我需要为此函数编写一个单元测试
public function action()
{
$data = $this->request->getParsedBody() ?? [];
$user = new User($data);
$this->userRepository->save($user);
}
我尝试模拟用户存储库界面
$app = $this->getAppInstance();
$container = $app->getContainer();
$user = new User(['a' => 'b']);
$userRepositoryProphecy = $this->prophesize(UserRepository::class);
$userRepositoryProphecy
->save($user)
->willReturn($user)
->shouldBeCalledOnce();
$container->set(UserRepository::class, $userRepositoryProphecy->reveal());
但返回
TypeError : Double\UserRepository\P1::save() 的返回值必须是 App\Domain\User\User 的实例,返回 null
我使用 slim-sketch 和 phpunit
慕斯709654