1

使用我生成的经典 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有一些额外的行 - 但它们不能解决问题。

再次感谢安德鲁

4

1 回答 1

0

好吧,看起来这个函数预计会从 getResponse 中获取 null。

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

如果没有可用的响应,此行应该阻止屏幕截图调用。然而它没有。看起来是个问题。您使用哪个 Symfony 版本?

于 2013-05-21T09:53:26.207 回答