2011-10-31 140 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

相關問題