我有一个适用于我的 symfony 应用程序的 phpunit 测试套件。在该测试文件中,我在不同的测试之间有一些依赖关系,并在依赖关系之间传递一个 DOMCrawler 对象,这样我就不必每次都导航到它。
但是,在采用我所做的方法时,您似乎无法使用这些传递的对象提交表单,但您可以单击它们上的链接。是否有一个原因?我的设计是否很糟糕?如果是,我应该如何改变它?欢迎任何反馈。我在下面附上了一些代码。
<?php
namespace someBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* blah Controller Test
*
*/
class BlahControllerTest extends WebTestCase
{
private $adminUrl;
/**
* Constructs basic information for a audit report controller test suite
*
*/
public function __construct()
{
$this->adminUrl = '/admin/';
}
/**
* Starts a test suite
*
* @return Crawler
*/
public function testAdd()
{
// Create a new client to browse the application
$client = static::createClient();
// Go to site specific admin url
$crawler = $client->request('GET', $this->adminUrl);
$this->assertTrue(200 === $client->getResponse()->getStatusCode());
// do stuff here
// goes to edit page
$crawler = $client->request('GET', $editPage);
return $crawler;
}
/**
* Tests the edit functionality
*
* @param Crawler $crawler Crawler for the show view
*
* @depends testAdd
*/
public function testEdit($crawler)
{
// Create a new client to browse the application
$client = static::createClient();
//Line below is included if the crawler points to the show view
//$crawler = $client->click($crawler->selectLink('Edit')->link());
// Fill in the form and submit it
$form = $crawler->selectButton('Edit')->form(array(
$foo => $bar,
));
// The following line doesn't work properly if testEdit is passed the
// edit page. However, if it is passed the show page, and the
// edit link above is clicked, then the form will submit fine.
$client->submit($form);
$crawler = $client->followRedirect();
// more code here...
}
}
原因是,正如您在WebTestCase
您扩展的类,实现了拆卸:
protected function tearDown()
{
if (null !== static::$kernel) {
static::$kernel->shutdown();
}
}
内核的关闭会产生很多影响。一种影响就是你所经历的。我试图追踪到底发生了什么,但我一无所获,只是在心里记下了,一旦调用关闭,客户端和爬虫就变得毫无用处。
我会推荐与路易斯相同的东西:让你的测试独立。除了它不与客户端一起工作之外,请考虑一下您的创建页面上出现问题的时间。实际上,您的编辑页面测试也会中断,尽管页面本身可能没问题。
Depends 通常用于进一步验证对象,例如您是否想更深入地测试响应。您将使用依赖测试并返回第一个测试的响应。在这种情况下,两个测试都中断也没关系,因为如果您的创建页面中断,您的响应内容当然不会看起来像它应该的那样。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)