使用 phpunit symfony 测试后删除测试实体
Delete test entity after testing with phpunit symfony
当我测试一个实体时,它会在数据库中创建它,但我无法删除它。我想我有删除实体的默认代码,但它不起作用,还有其他方法吗?我错过了什么吗?
这是代码。我正在使用 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());
}
这段代码实际上向我们展示了您如何测试 UI,但实际删除实体的真实代码...
因此,您应该首先检查这两种情况(添加实体和删除实体)是否确实在单元测试中正常工作(例如,也许在删除实体时您没有刷新更改...) .
然后,当您证明自己可以实际添加和删除实体并且控制器正常工作时,您应该测试您的用户界面,这就是您向我们展示的内容。
所以,如果您已经这样做了,问题出在您的 UI 上(例如,您的按钮无法被点击)。
也许能提供更多信息?
我错过了这个从数据库中删除实体的方法
/**
* Close doctrine connections to avoid having a 'too many connections'
* message when running many tests
*/
public function tearDown(){
parent::tearDown();
}
当我测试一个实体时,它会在数据库中创建它,但我无法删除它。我想我有删除实体的默认代码,但它不起作用,还有其他方法吗?我错过了什么吗?
这是代码。我正在使用 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());
}
这段代码实际上向我们展示了您如何测试 UI,但实际删除实体的真实代码...
因此,您应该首先检查这两种情况(添加实体和删除实体)是否确实在单元测试中正常工作(例如,也许在删除实体时您没有刷新更改...) .
然后,当您证明自己可以实际添加和删除实体并且控制器正常工作时,您应该测试您的用户界面,这就是您向我们展示的内容。
所以,如果您已经这样做了,问题出在您的 UI 上(例如,您的按钮无法被点击)。
也许能提供更多信息?
我错过了这个从数据库中删除实体的方法
/**
* Close doctrine connections to avoid having a 'too many connections'
* message when running many tests
*/
public function tearDown(){
parent::tearDown();
}