2011-10-31 122 views
3

我正在运行PHP 5.3.6和来自Github的PHPUnit的最新版本。当我从文档复制示例17.1时,它在assertTitle失败时会发生致命错误。我得到这个错误信息:PHPUnit,硒基本测试因致命错误而失败

Fatal error: Call to a member function toString() on a non-object in <path>/phpunit/phpunit-selenium/PHPUnit/Extensions/SeleniumTestCase.php on line 1041 

当我改变断言通过,PHPUnit运行得很好。

我挖了线,这是片段:

protected function onNotSuccessfulTest(Exception $e) 
    { 
     if ($e instanceof PHPUnit_Framework_ExpectationFailedException) { 
      $buffer = 'Current URL: ' . $this->drivers[0]->getLocation() . 
         "\n"; 
      $message = $e->getComparisonFailure()->toString(); 

$ E-> getComparisonFailure()返回NULL,而不是对象。我做错了什么?

UPDATE:

我找到了失败的原因,虽然修复不在我的视野呢。

在PHPUnit的92线/框架/ Constraint.php,它调用使用

$this->fail($other,$description)

其被定义为:

protected function fail($other, $description, PHPUnit_Framework_ComparisonFailure $comparisonFailure = NULL)

由于没有PHPUnit_Framework_ComparisonFailure通过,NULL是传递给PHPUnit/Framework/Constraint.php的第141行

抛出新PHPUnit_Framework_ExpectationFailedException( $ failureDescription, $ comparisonFailure );

它被getComparisonFailure()取回,它应该返回一个对象,如上所述。

还有什么想法?

+0

你说phpunit-selenium的默认复制粘贴示例有一个错误,当它的唯一断言失败时,复制和粘贴应该失败? –

回答

4

只是$message = $e->getComparisonFailure()->exceptionToString;

更换$message = $e->getComparisonFailure()->toString();它工作正常,我

+0

这对我不起作用,因为$ e-> getComparisonFailure返回的对象是NULL,而不是具有“exceptionToString”作为方法的对象。 –

+0

我将它从exceptionToString()更改为exceptionToString并且它工作正常。这是PHPUnit中的一个需要拉取请求的错误吗? –

1

我更换$message = $e->getComparisonFailure()->toString();是:

if($e->getComparisonFailure() == NULL) 
{ 
    $message = $e; 
} 
else 
{ 
    $message = $e->getComparisonFailure()->toString(); 
} 

我也恢复PHPUnit_Framework_ExpectationFailedException回3.5.3所以它有setCustomMessage()

3

此问题与PHPU上的issue #66有关Github上的nit-Selenium项目。我做了一个修复这个问题的提交,修复了Matthew提到的"setCustomMessage()" problem。我觉得哪里哪里改为

// ?: Is "comparison failure" set and therefore an object? (fixes issue #66) 
if(is_object($e->getComparisonFailure())) { 
    // -> Comparison failure is and object and should therefore have the toString method 
    $message = $e->getComparisonFailure()->toString(); 
} 
else { 
    // -> Comparison failure is not an object. Lets use exception message instead 
    $message = $e->getMessage(); 
} 

而在onNotSuccessfulTest()结束通过one commit in PHPUnit 3.6

$message = $e->getComparisonFailure()->toString(); 

介绍两个问题:

$e->setCustomMessage($buffer); 

哪里改为:

// ?: Does the setCustomMessage method exist on the exception object? (fixes issue #67) 
if(method_exists($e, 'setCustomMessage')) { 
    // -> Method setCustomMessage does exist on the object, this means we are using PHPUnit 
    // between 3.4 and 3.6 
    $e->setCustomMessage($buffer); 
} 
else { 
    // -> Method setCustomerMessages does not exist on the object, must make a new exception to 
    // add the new message (inc. current URL, screenshot path, etc) 
    $e = new PHPUnit_Framework_ExpectationFailedException($buffer, $e->getComparisonFailure()); 
} 

希望这两个更改都将被接受到PHPUnit-Selenium项目中。至少它解决了我在使用来自PEAR的PHPUnit和PHPUnit-Selenium的最新更新时遇到的问题。

+0

最佳答案 - 12/14/2011 – jchapa

相关问题