简体   繁体   中英

Right assert for testing Doctrine entities in Symfony2

I'm writing some test for my entities and this is the code:

$device = new Device();
$strTool = new StringTools();

$imei = $strTool->randomNumber(17);
$device->setImei($imei);
$device->setDescription($strTool->generateRandomString(50));

$this->em->persist($device);
$this->em->flush();

$devices = $this->em->getRepository('DeviceBundle:Device')->findOneBy(array('imei' => $imei));

$this->assertCount(1, $devices);
$this->assertTrue(is_object($device));

But first test assertCount is failing with this message:

PHPUnit_Framework_Exception: Argument #2 (No Value) of PHPUnit_Framework_Assert::assertCount() must be a countable or traversable

What is the right way to test I get results from DB?

findOneBy return only one element, use findBy instead.

btw, what you are testing here is Doctrine itself. Doctrine is pretty well tested so there is no need for you to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM