sách gpt4 ai đã đi

php - 为什么 symfony DOMCrawler 对象没有在依赖的 phpunit 测试之间正确传递?

In lại 作者:行者123 更新时间:2023-12-04 05:40:33 25 4
mua khóa gpt4 Nike

我有一个用于 symfony 应用程序的 phpunit 测试套件。在那个测试文件中,我在不同的测试之间有一些依赖关系,并在依赖之间传递一个 DOMCrawler 对象,这样我就不必每次都导航到它。

但是,在采用我所做的方法时,您似乎无法使用这些传递的对象提交表单,但您可以单击它们上的链接。这有什么原因吗?我的设计是不是很差,如果是,我应该如何改变它?欢迎任何反馈。我在下面附上了一些代码。


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...
}
}

1 Câu trả lời

原因是,正如您在 WebTestCase 中看到的那样您扩展的类,实现了拆卸:

protected function tearDown()
{
if (null !== static::$kernel) {
static::$kernel->shutdown();
}
}

内核的这种关闭有很多影响。一种影响是你正在经历的。我曾试图追踪到底发生了什么,但我什么也没找到,只是在心里记下了一旦调用关闭,客户端和爬虫就毫无用处。

我会推荐和 Louis 一样的东西:让你的测试独立。除了它不与客户端一起工作之外,请考虑创建页面上出现问题的时间。实际上,您的编辑页面测试也会中断,尽管页面本身可能没问题。

Depends 通常用于进一步验证对象,例如您是否想更深入地测试响应。您将使用依赖测试并从第一个返回响应。在这种情况下,两个测试都中断也是可以的,因为如果您创建分页符,当然您的响应内容看起来不应该如此。

关于php - 为什么 symfony DOMCrawler 对象没有在依赖的 phpunit 测试之间正确传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11311744/

25 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com