1

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 ?

shuba.ivan
  • 3,824
  • 8
  • 49
  • 121
  • Did you try with `$this->getClient()->getContainer()->set('app.additional_function', $mock);`? – A.L Jul 26 '18 at 12:56

1 Answers1

-1

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);
Massimiliano Arione
  • 2,422
  • 19
  • 40