【问题标题】:PHPUnit The current node list is emptyPHPUnit 当前节点列表为空
【发布时间】:2015-05-19 14:34:03
【问题描述】:

我正在创建一些功能测试来测试我的控制器。我目前有 2 个功能。 1 用于登录,1 用于实体生命周期。

两者都应该正常运行(我猜)。但是我收到以下错误:

当前节点列表为空

我尝试从测试类中删除我的所有代码,但没有结果。我还尝试添加一个空方法,看看它是否也发生在那里。是的。该测试也崩溃了。

所以我搜索了一个解决方案。我尝试了一些事情,例如:

($client = static::createClient(array(), array('HTTP_HOST' => 'symfony.dev'));)

var_dump($client->getResponse()->getContent());(获取应该进行测试的页面)

Change the header:

$response = $this->render('CMSBundle:Front:test.html.twig',);
$response->headers->set('Content-Type', 'text/html');       

return $response;

还有其他一些事情。他们都没有工作。我似乎无法弄清楚问题所在。

有人对这个问题有什么建议吗?

我的测试代码:

public function testLogin()
{
    $client = $this->client;

    $crawler = $client->request('GET', '/admin/login');
    $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /admin/login");

    // Fill in the form and submit it
    $form = $crawler->selectButton('Inloggen')->form(array(
        '_username'  => 'erik',
        '_password'  => 'erik'
    ));
    $client->submit($form);
}

提前致谢!

【问题讨论】:

    标签: php symfony testing phpunit


    【解决方案1】:

    经过一番搜索,我在stackoverflow 上找到了答案。

    默认情况下,symfony2 爬虫会猜测服务器的位置在“localhost”。然而我的不在那儿。所以我不得不告诉爬虫去哪里看。

    这是我必须添加的代码'HTTP_HOST' => 'my.server.location'

    添加此代码使代码如下所示:

    $this->client = static::createClient( 
         array(),
         array(
            'HTTP_HOST' => 'my.server.location', //dependent on server        
        ));
    $this->client->followRedirects(true);
    

    所以现在当我得到 url $crawler = $client->request('GET', '/admin/login'); 时,爬虫会得到以下 url:http://my.server.location/admin/login

    希望这对任何人都有帮助!

    感谢 Matt 提供答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-14
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      相关资源
      最近更新 更多