1
votes

I use Symfony 3.4. I have functional tests which extend from Liip\FunctionalTestBundle\Test\WebTestCase and in some function I want mock some service, I create mockobject for service but in action I still have original service, how to mock service

in my function

        $mock = $this->getMockBuilder(AdditionalFunction::class)
        ->disableOriginalConstructor()->getMock();

    $this->getContainer()->set('app.additional_function', $mock);


    $this->getClient()->request(
        Request::METHOD_GET,
        $uri
    );

and in my action $uri

public function sendAction(OutboundInvoice $outboundInvoice)
{
    $test = $this->get('app.additional_function');

in variable $test I had original class ‌AppBundle\Helper\AdditionalFunction

How to mock service ?

1
Did you try with $this->getClient()->getContainer()->set('app.additional_function', $mock);? - A.L

1 Answers

-1
votes

You can do the following, as suggested in https://github.com/liip/LiipFunctionalTestBundle/issues/107#issuecomment-358267059

$mock = $this->createMockInAnyWayYouWant();
static::$kernel->getContainer()->set('id_service', $mock);