2016-04-28 68 views
0

我试图在Symfony 2.8应用程序上用PHPUnit执行一个简单的测试。 这是测试:尝试在Symfony 2.8中执行PHPUnit测试时出错

public function testCreateContact(){ 
     $client= static::createClient(); 
     $client->followRedirects(); 
     $crawler = $client->request('GET', '/contact_create'); 
     $link = $crawler->filter('a:contains("Cancel")')->link(); 
     $crawlerLink = $client->click($link); 

    } 

而且树枝文件:

{% extends '::layout.html.twig' %} 

{% block title %}MaestroBundle:Contacto:alta{% endblock %} 

{% block body %} 

<h1>New Contacto</h1> 
<div class="form_error"></div> 
<div id="form_body"> 
    <form id="createForm" method="POST" {{ form_enctype(form) }}> 
      {{ form_widget(formulario) }} 
      <input type="submit" value="create"/> 
     </form> 
      <a id="cancel" name="cancel" href="{{ path('contact_show') }}">Cancel</a> 

</div> 
{% endblock %} 

当我执行这项测试中,我得到以下错误:

There was 1 error:

1) SisEvo\MaestroBundle\Tests\Controller\ContactoControllerTest::testCrearContacto InvalidArgumentException: The current node list is empty.

/var/www/html/evoIsaac/vendor/symfony/symfony/src/Symfony/Component/DomCrawler/Crawler.php:706 /var/www/html/evoIsaac/src/SisEvo/MaestroBundle/Tests/Controller/ContactoControllerTest.php:24

FAILURES! Tests: 2, Assertions: 1, Errors: 1.

这是行号24 : $ link = $ crawler-> filter('a:contains(“Cancel”)') - > link();

此外,我试着检查$履带含量的var_dump,这就是结果:

The file "/var/www/html/evoIsaac/app/config/routing.yml" does not contain valid YAML in /var/www/html/evoIsaac/app/config/routing.yml (which is being imported from "/var/www/html/evoIsaac/app/config/routing_dev.yml"). (500 Internal Server Error)

PHPUnit的版本:5.3.2 PHP版本:5.6.18 Symfony的版本:2.8.4 操作系统:Fedora 23

有人可以帮助我吗?

编辑

我想检查我的YAML文件,我得到这个错误:

ERROR:

while scanning for the next token found character '@' that cannot start any token in "", line 22, column 15: resource: @MaestroBundle/Controller/ ^

这是我的YAML文件:

cys: 
    resource: "@CysBundle/Controller/" 
    type:  annotation 
    prefix: /

homepage: 
    path:/
    defaults: 
     _controller: FrameworkBundle:Template:template 
     template: 'default/login.html.twig' 


contrato_evolutia: 
    resource: "@ContratoEvolutiaBundle/Controller/" 
    type: annotation 

api_maestro: 
    resource: "@ApiBundle/Controller/Maestro/" 
    type:  annotation 

maestro: 
    resource: @MaestroBundle/Controller/ 
    type:  annotation 

default: 
    resource: @DefaultBundle/Controller/ 
    type:  annotation 


fos_js_routing: 
    resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml" 

我也试图检查客户回应:

$this->assertTrue($client->getResponse()->isSuccessful()); 

但PHPUnit的显示此错误:

Failed asserting that false is true

希望有人能帮助我。

+0

如错误消息中所述,您在'app/config/routing.yml'中有错误。你可以用一个在线的YAML验证器来检查它:http://yaml-online-parser.appspot.com/ –

+1

作为一个注释,在检查其他东西之前,请验证客户端请求是否正确,例如'$ this-> assertTrue $ client-> getResponse() - > isSuccessful());' – Matteo

+0

谢谢你,我试过你的解决方案,并用结果编辑问题。 –

回答

0

最后我解决了这个问题! 谢谢@ A.L和@Matteo的评论;问题是我在@Bundle之前在YAML中没有“”,现在它正在工作!

我YAML文件的最终版本是:

cys: 
    resource: "@CysBundle/Controller/" 
    type:  annotation 
    prefix: /

homepage: 
    path:/
    defaults: 
     _controller: FrameworkBundle:Template:template 
     template: 'default/login.html.twig' 


contrato_evolutia: 
    resource: "@ContratoEvolutiaBundle/Controller/" 
    type: annotation 

api_maestro: 
    resource: "@ApiBundle/Controller/Maestro/" 
    type:  annotation 

maestro: 
    resource: "@MaestroBundle/Controller/" 
    type:  annotation 

default: 
    resource: "@DefaultBundle/Controller/" 
    type:  annotation 


fos_js_routing: 
    resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml" 

非常感谢您! 艾萨克。