【问题标题】:Delete test entity after testing with phpunit symfony用 phpunit symfony 测试后删除测试实体
【发布时间】:2016-01-30 08:36:25
【问题描述】:

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

这里是代码。我正在使用 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());

}

【问题讨论】:

    标签: symfony phpunit


    【解决方案1】:

    这段代码实际上是在向我们展示你如何测试 UI,但真正的代码实际上是在删除实体......

    因此,您首先应该检查两种方案(添加实体和删除实体)是否在单元测试中实际正常工作(例如,当删除您没有刷新更改的实体时...)。

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

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

    也许更多的信息?

    【讨论】:

    • 我刚刚解决了这个调用这个方法:parent::tearDown();
    • 很高兴听到这个消息:D
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-17
      • 2016-10-30
      • 2015-04-26
      • 2019-11-22
      • 2018-03-18
      • 2015-01-14
      • 1970-01-01
      • 2017-08-16
      相关资源
      最近更新 更多