我試圖模擬EntityRepository
的find
方法,以便測試不查找數據庫中的數據,但似乎不起作用。下面是測試類的setUp
方法如何在Symfony 3中模擬「查找」方法
public function setUp()
{
parent::setUp();
$this->client = static::createClient();
$this->peopleManager = $this->getMockBuilder(PeopleManager::class)
->setMethods(['createPerson','peopleUpdate', 'peopleDelete', 'peopleRead'])
->disableOriginalConstructor()
->getMock();
$this->repository = $this->getMockBuilder(EntityRepository::class)
->disableOriginalConstructor()
->getMock();
$this->em = $this->getMockBuilder(EntityManager::class)
->disableOriginalConstructor()
->getMock();
}
這就是我們所說的查找功能的方法
public function updatePersonAction($id, Request $request)
{
$repository = $this->getDoctrine()->getRepository('GeneralBundle:People');
$person= $repository->find($id);
if($person)
{
$data = $request->request->get('array');
$createdPeople = array();
$UpdatedPerson = "";
foreach($data as $content)
{
$prueba = $this->get('people.manager');
$UpdatedPerson = $prueba->peopleUpdate(
$person,
$content['name'],
$content['surname'],
$content['secondSurname'],
$content['nationality'],
$content['birthday'],
$content['identityCard'],
$content['identityCardType']
);
array_push($createdPeople, $person);
}
$serializedEntity = $this->get('serializer')->serialize($UpdatedPerson, 'json');
return new Response($serializedEntity);
} else {
$serializedEntity = $this->get('serializer')->serialize('Doesn\'t exists any person with this id', 'json');
return new Response($serializedEntity);
}
}
調試表明peoplemanager類嘲笑,但它並沒有嘲笑實體管理器和存儲庫。
謝謝< 3.
的嘲笑它相當瑣碎http://docs.mockery.io/en/latest/試試看像'$ repo-> shouldReceive( '發現') - >一次() - >和返回([1,2,3]);' – Confidence
謝謝,這是非常有用的! –