简体   繁体   中英

Symfony, Call service in PHP Unit

I have a unit test, where I need to call a service.

I did it that way:

<?php
namespace FM\PriceBundle\Tests\Service;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class PriceServiceTest extends KernelTestCase
{
    private $container;

    public function testFiulPrice()
    {
        self::bootKernel();
        $this->container = self::$kernel->getContainer();

        $productId = 1;
        $id = 1;
        $what = ['postal', 'departement', 'region', 'country', 'insee'];
        $date = new \DateTime('2016-06-23');

        $price = $this->container->get('fm.price.get')->fiulPrice($productId, $id, $what[0], $date);

        $this->assertNotEmpty($price);
    }
}

But I have this error:

There was 1 error:

1) FM\\PriceBundle\\Tests\\Service\\PriceServiceTest::testFiulPrice Predis\\Connection\\ConnectionException: Connection refused [tcp://localhost:6379]

I guess you use redis in your app.

But I don't understand why do you want to implement test like this.

That test literally takes fixed data, an tries to get a result from external service that uses dependency (redis).

In my opinion, you need unit test for PriceService, another one for serivce registered as 'fm.price.get', and (maybe) the third one that use redis, but in this case you have to populate cache with some sample data.

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