codeception symfony2单元测试会引发致命错误

时间:2013-05-21 00:56:33

标签: symfony phpunit

使用我生成的经典phpunit样式:使用symfony2帮助程序进行测试。 我可以得到服务,我可以断言好。

//测试     public function testGetServiceUrl(){         $ ecservice = $ this-> getModule('Symfony2') - > grabServiceFromContainer(“ecservice”);

    $this->assertEquals("https://ecoconnect2.niwa.co.nz/services", $ecservice->getServiceUrl());
    $this->assertEquals("xxx", $ecservice->getServiceUrl());

}

然而,在断言失败的第二种情况下,我得到一个例外:

    Trying to test get service url (demoTest::testGetServiceUrl) - Failed
    PHP Fatal error:  Call to a member function getResponse() on a non-object in /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/Util/Framework.php on line 30
    PHP Stack trace:
    PHP   1. {main}() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/codecept:0
    PHP   2. Symfony\Component\Console\Application->run() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/codecept:38
    PHP   3. Symfony\Component\Console\Application->doRun() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:106
    PHP   4. Symfony\Component\Console\Command\Command->run() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:193
    PHP   5. Codeception\Command\Run->execute() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:240
    PHP   6. Codeception\Codecept->runSuite() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/Command/Run.php:74
    PHP   7. Codeception\SuiteManager->run() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/Codecept.php:110
    PHP   8. Codeception\PHPUnit\Runner->doEnhancedRun() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/SuiteManager.php:132
    PHP   9. PHPUnit_Framework_TestSuite->run() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/PHPUnit/Runner.php:107
    PHP  10. PHPUnit_Framework_TestSuite->runTest() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:745
    PHP  11. PHPUnit_Framework_TestCase->run() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:775
    PHP  12. PHPUnit_Framework_TestResult->run() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:776
    PHP  13. PHPUnit_Framework_TestResult->addFailure() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php:698
    PHP  14. Codeception\PHPUnit\Listener->addFailure() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php:307
    PHP  15. Codeception\PHPUnit\Listener->fire() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/PHPUnit/Listener.php:24
    PHP  16. Symfony\Component\EventDispatcher\EventDispatcher->dispatch() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/PHPUnit/Listener.php:66
    PHP  17. Symfony\Component\EventDispatcher\EventDispatcher->doDispatch() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:53
    PHP  18. call_user_func() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:164
    PHP  19. Codeception\Subscriber\Module->failed() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:164
    PHP  20. Codeception\Util\Framework->_failed() /Users/watkinsav/workspace/cd/ecoconnect_web/vendor/codeception/codeception/src/Codeception/Subscriber/Module.php:47

这是因为Framework.php中的_failed()函数尝试调用client-getResponse()。我们没有实例化响应类。

public function _failed(\Codeception\TestCase $test, $fail)
 {
     if (!$this->client->getResponse()) return;
     file_put_contents(\Codeception\Configuration::logDir() . basename($test->getFileName()) . '.page.debug.html',  $this->client->getResponse()->getContent());
 }

在此处的示例博客文章中:http://codeception.com/02-12-2013/testing-symfony2.html还有一些额外的行 - 但它们无法解决问题。

再次感谢Andrew

1 个答案:

答案 0 :(得分:0)

好吧,看起来这个函数应该从getResponse得到null。

if (!$this->client->getResponse()) return;

如果没有可用的响应,此行应该已阻止屏幕截图调用。 但事实并非如此。看起来像一个问题。你使用哪个Symfony版本?