我正在开发一个Symfony4应用程序,并想测试服务是否确实更新了我的数据库。
这里我给出一个数据传输对象
public function update(ChargeDto $chargeDto): ChargeDto
{
$charge = $this->chargeRepository->find($chargeDto->id);
// The mapper fills the new data into my entity
$charge = ChargeMapper::fromDto($charge, $chargeDto);
// here I check that the entity has in fact the new data
VarDumper::dump($charge->getAuthorPNumber());
$this->entityManager->persist($charge);
$this->entityManager->flush();
$this->entityManager->refresh($charge);
// here I check that the entity still has the new data
VarDumper::dump($charge->getAuthorPNumber());
return ChargeMapper::toDto($charge, $chargeDto);
}
在测试类中,我从db获取更新的数据集,以检查它是否确实更新过:
$res = $this->chargeRepo->find($updatedDto->id);
VarDumper::dump($res->getAuthorPNumber());
我得到了旧数据。如果我手动查看数据库,实际上它已更新并且包含新值。
我不知道任何缓存都处于活动状态。我的测试如何获得真正的新值和更新值?
PS:我只是仔细检查了一下:对于产品环境,我有一个用于理论缓存的配置(/config/prod/doctrine.yaml),但是对于开发环境和测试环境都没有。
您entityManager
应该具有一个称为clear的函数,该函数将删除缓存的对象。(确保仅在测试中而不是在实际代码中调用此函数)
您还可以通过将EntityName作为参数传递给函数来从缓存中仅清除一种类型的实体。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句