Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何使用Symfony在serviceTest中模拟EntityManager?_Unit Testing_Phpunit_Symfony - Fatal编程技术网

Unit testing 如何使用Symfony在serviceTest中模拟EntityManager?

Unit testing 如何使用Symfony在serviceTest中模拟EntityManager?,unit-testing,phpunit,symfony,Unit Testing,Phpunit,Symfony,对不起,我的问题很长 如何在扩展内核测试用例的服务测试中模拟entityManager? 现在,解释和我的测试 我使用的是Symfony3.2。我的申请是标准的。我有一些控制器,我使用WebTestCase来测试它们 一般来说,我的控制器验证参数,调用服务/a管理器,处理一些变量并将它们推送到视图,我的测试在测试扩展WebTestCase中非常简单 /** * Test New Game Action */ public function testFooAction(){ //We

对不起,我的问题很长

如何在扩展内核测试用例的服务测试中模拟entityManager?

现在,解释和我的测试

我使用的是Symfony3.2。我的申请是标准的。我有一些控制器,我使用WebTestCase来测试它们

一般来说,我的控制器验证参数,调用服务/a管理器,处理一些变量并将它们推送到视图,我的测试在测试扩展WebTestCase中非常简单

/**
 * Test New Game Action
 */
public function testFooAction(){
    //We mock the Service
    $fooService = $this
        ->getMockBuilder(GameService::class)
        ->disableOriginalConstructor()
        ->getMock();
    $fooService->expects(self::once())
        ->method('barMethod')
        ->willReturn($result);

    //We create the client
    $client = static::createClient();
    $container = $client->getContainer();

    //I put my mock here
    $container->set('app.game-service', $fooService);

    //I launch the request
    $client->request('GET', '/foo');
    //I handle the response
    $response = $client->getResponse();

    //I do some tests like this one
    self::assertEquals(200, $response->getStatusCode());

}
如您所见,我不调用EntityManger,因为我使用服务和这些行来模拟我的服务

    //We create the client
    $client = static::createClient();
    $container = $client->getContainer();

    //I put my mock here
    $container->set('app.game-service', $fooService);
在我的服务中模拟实体管理器有问题。我的控制器已测试,但我的服务未测试

下面是初始化了一个简单的受保护的属性entityManager的构造函数。问题是这是受保护的,您将进一步看到

   /**
     * FooService constructor.
     * @param EntityManager $entityManager
     */
    public function __construct(EntityManager $entityManager)
    {
           $this->entityManager = $entityManager;
    }
要测试我的服务,以下是我的初始代码:

<?php
class FooServiceTest extends KernelTestCase
{
    /**
     * Foo Service.
     *
     * @var FooService
     */
    private $fooService;

    /**
     * Prepares the environment before running each test.
     */
    protected function setUp()
    {
        parent::setUp();

        self::bootKernel();
        $this->fooService = static::$kernel->getContainer()
            ->get('app.foo-service') //HERE IS HOW I HANDLE MY SERVICE TO TEST IT
        ;

    }
这些代码不起作用。Mock没有到位。我还有真正的实体经理。我想是因为集装箱已经关好了。设置是否有用

像野蛮人一样,我将entityManager属性更改为公共

我这样做:

    protected function setUp()
    {
        parent::setUp();

        $entityManagerMock = $this
            ->getMockBuilder('Doctrine\ORM\EntityManager')
            ->disableOriginalConstructor()
            ->getMock();
        $entityManagerMock
            ->expects(once())  // I WANT EXACTLY ONE CALL TO commit METHOD
            ->method('commit')  
            ->willReturn(null);

        self::bootKernel();
        $this->gameService = static::$kernel->getContainer()
            ->get('app.game-service')
        ;
        $this->gameService->entityManager = entityManagerMock;
它工作得很好。测试可以运行。但在公共财产中设置实体管理人并不是一个好的做法

我的问题是:如何在服务测试中模拟entityManager


(对不起,我的英语不流利)

首先在你的代码中

$container->set('doctrine.orm.entity_manager'); // THIS LINE DOES NOTHING <=======

$container->set('doctrine.orm.entity_manager');//这条线没用,谢谢你的帮助!我不太明白你这句话的意思:用EntityManager封装一些适配器。你能编辑你的答案给我一个简短的例子吗?我认为你应该选择你的测试,要么是单元测试(你将模拟你的依赖关系),要么是WebTestCase,在这种情况下,你应该使用测试数据库并正常使用你的应用程序而不进行模拟。我还喜欢“像个野蛮人一样,我将entityManager属性更改为public”
$container->set('doctrine.orm.entity_manager'); // THIS LINE DOES NOTHING <=======