【问题标题】:symfony test database insertsymfony 测试数据库插入
【发布时间】:2013-10-16 14:40:17
【问题描述】:

我有一个功能测试,它在数据库中创建和保留一些东西,我想测试插入的项目数量是否正确(有一种情况,它当前插入两个而不是一个)。

在控制器中一切似乎都正常工作,如果我使用下面的代码(在控制器中)进行调试,我会得到“2”的预期(错误)值:

$em = $this->getDoctrine()->getManager();
$fooRepo = $em->getRepository('CompanyProjectBundle:Foo');
$foos = $fooRepo->retrieveByBar(3);
echo count($foos); // Gives a result of 2

但是,如果我在我的测试课程中尝试类似的东西,我会得到零...

/**
 * {@inheritDoc}
 */
protected function setUp()
{
    static::$kernel = static::createKernel();
    static::$kernel->boot();
    $this->em = static::$kernel->getContainer()
        ->get('doctrine')
        ->getManager()
    ;
    $this->em->getConnection()->beginTransaction();
}

/**
 * {@inheritDoc}
 */
protected function tearDown()
{
    parent::tearDown();
    $this->em->getConnection()->rollback();
    $this->em->close();
}

public function testFooForm()
{
    // ... do some testing

    $fooRepo = $this->em->getRepository('CompanyProjectBundle:Foo');
    $foos = $fooRepo->retrieveByBar(3);
    echo count($foos); // gives a result of ZERO

    // ... more happens later
}

它是否获得了不同的实体管理器或类似的东西?我是否应该使用其他方法来获取正确的 EM,以便我可以查看运行应用程序的相同数据?

一切都在事务中运行(当测试客户端被销毁时会回滚),但这发生在上面显示的 sn-p 之后。

【问题讨论】:

    标签: symfony testing doctrine-orm functional-testing


    【解决方案1】:

    啊...解决了我自己的问题。我想我弄错了EntityManager。我通过客户端的容器而不是内核的容器获取 EntityManager 来修复它:

    public function testFooForm()
    {
        // ... do some testing
    
        $clientEm = $client->getContainer()->get('doctrine.orm.entity_manager');
        $fooRepo = $clientEm->getRepository('CompanyProjectBundle:Foo');
        $foos = $fooRepo->retrieveByBar(3);
        echo count($foos); // gives the correct result of 2
    
        // ... more happens later
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-08
      • 2019-07-21
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 2021-11-04
      • 2019-07-30
      相关资源
      最近更新 更多