【问题标题】:PHPUnit, Selenium Basic Test Fails with Fatal ErrorPHPUnit,Selenium 基本测试失败并出现致命错误
【发布时间】:2011-10-31 05:47:03
【问题描述】:

我正在运行 PHP 5.3.6 和来自 Github 的最新版本的 PHPUnit。当我从文档中复制示例 17.1 时,它会在 assertTitle 失败时遇到致命错误。我收到此错误消息:

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 运行良好。

我挖出了这条线,这是 sn-p :

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,而不是对象。我做错了什么?

更新:

我找到了失败的原因,尽管我还没有找到解决办法。

在PHPUnit/Framework/Constraint.php的第92行,它调用使用

$this-&gt;fail($other,$description)

定义为:

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

由于没有传递 PHPUnit_Framework_ComparisonFailure,所以在 PHPUnit/Framework/Constraint.php 的第 141 行传递了NULL

 

抛出新的 PHPUnit_Framework_ExpectationFailedException( $故障描述, $比较失败 );

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

还有什么想法吗?

【问题讨论】:

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

标签: php selenium phpunit tostring


【解决方案1】:

只需将$message = $e-&gt;getComparisonFailure()-&gt;toString(); 替换为$message = $e-&gt;getComparisonFailure()-&gt;exceptionToString;

对我来说很好用

【讨论】:

  • 这对我不起作用,因为 $e->getComparisonFailure 返回的对象是 NULL,而不是具有“exceptionToString”作为方法的对象。
  • 我将它从 exceptionToString() 更改为 exceptionToString 并且它有效。这是 PHPUnit 中需要拉取请求的错误吗?
【解决方案2】:

这个问题与 Github 上 PHPUnit-Selenium 项目中的 issue #66 有关。我做了一个修复这个问题的提交,一个修复了马修提到的"setCustomMessage()" problem。我认为one commit in PHPUnit 3.6引入的这两个问题

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

改到哪里

// ?: 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() 结束时:

$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 的最新更新中遇到的问题。

【讨论】:

    【解决方案3】:

    我将$message = $e-&gt;getComparisonFailure()-&gt;toString(); 替换为:

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

    我还将 PHPUnit_Framework_ExpectationFailedException 恢复到 3.5.3,所以它有 setCustomMessage()

    【讨论】:

      猜你喜欢
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 2015-09-19
      • 2021-05-16
      • 2020-08-15
      • 2015-08-05
      • 1970-01-01
      相关资源
      最近更新 更多