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

时间:2016-01-29 23:52:19

标签: symfony phpunit

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

这是代码。我使用的是symfony 2.7.8和Php unit 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());

}

2 个答案:

答案 0 :(得分:1)

此代码实际上向我们展示了如何测试UI,但实际上是删除实体的真实代码......

因此,您首先应该检查两种情况(添加实体并删除它)实际上是否正常使用单元测试(例如,当删除实体时,您没有刷新更改,例如......)。 / p>

然后,当您证明自己可以实际添加和删除实体,并且控制器正在运行时,您应该测试您的用户界面,这就是您向我们展示的内容。

因此,如果您已经完成此操作,则问题出在您的用户界面上(例如,您的按钮无法跟踪)。

也许更多的信息?

答案 1 :(得分:0)

我错过了从数据库中删除实体的方法

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

        parent::tearDown();
    }