使用phpunit symfony进行测试后删除测试实体

使用phpunit symfony进行测试后删除测试实体,symfony,phpunit,Symfony,Phpunit,当我测试一个实体时,它会在数据库中创建它,但我无法删除它。我想我有删除实体的默认代码,但它不起作用,还有其他方法吗?我错过什么了吗 这是代码。我使用的是symfony2.7.8和Php单元4.8.0 public function testCreateCurso() { // Create a new client to browse the application $client = static::createAuthorizedClient();

当我测试一个实体时,它会在数据库中创建它,但我无法删除它。我想我有删除实体的默认代码,但它不起作用,还有其他方法吗?我错过什么了吗

这是代码。我使用的是symfony2.7.8和Php单元4.8.0

 public function testCreateCurso()
    {
        // Create a new client to browse the application
        $client = static::createAuthorizedClient();

    // Create a new entry in the database
    $crawler = $client->request('GET', '/admin/curso/');
    $this->assertEquals(200, $client->getResponse()->getStatusCode(), 'Unexpected HTTP status code for GET /curso/');

    $crawler = $client->click($crawler->selectLink('Crear Nuevo Curso')->link());

    // Fill in the form and submit it
    $form = $crawler->selectButton('Create')->form(array(
        'appbundle_curso[nombreCurso]' => 'Test',
        'appbundle_curso[codigoCurso]' => 'Test4',
        // ... other fields to fill
    ));

    $client->submit($form);


    $this->assertTrue($client->getResponse()->isRedirect());
    $crawler = $client->followRedirect();


    // Check data in the show view
    $this->assertGreaterThan(0, $crawler->filter('td:contains("Test")')->count(), 'Missing element td:contains("Test")');

    // Edit the entity
    $crawler = $client->click($crawler->selectLink('Editar')->link());

    $form = $crawler->selectButton('Update')->form(array(
        'appbundle_curso[nombreCurso]' => 'Foo',
        'appbundle_curso[codigoCurso]' => 'Foo1',
        // ... other fields to fill
    ));

    $client->submit($form);
    //
    $crawler = $client->followRedirect();

    // Check the element contains an attribute with value equals "Foo"
    $this->assertGreaterThan(0, $crawler->filter('[value="Foo"]')->count(), 'Missing element [value="Foo"]');

    // Delete the entity
    $client->submit($crawler->selectButton('Delete')->form());
    $crawler = $client->followRedirect();

    // Check the entity has been delete on the list
    $this->assertNotRegExp('/Foo/', $client->getResponse()->getContent());
    var_dump($client->getResponse()->getContent());

}

这段代码实际上向我们展示了如何测试UI,但实际删除实体的代码

因此,您应该首先检查这两种场景(添加实体和删除实体)是否在单元测试中都正常工作(例如,在删除实体时,您可能没有刷新更改…)

然后,当你展示了你自己,你可以添加和删除实体,并且控制器工作时,你应该测试你的用户界面,这就是你展示给我们的

因此,如果您已经这样做了,那么问题就出在您的UI上(例如,您的按钮无法跟随)


可能需要更多的信息吗?

我缺少从数据库中删除实体的方法

   /**
     * Close doctrine connections to avoid having a 'too many connections'
     * message when running many tests
     */
    public function tearDown(){

        parent::tearDown();
    }

我刚刚通过调用这个方法解决了这个问题:
parent::tearDown()很高兴听到:D