PHPUnit - 在外部对象中使用断言

PHPUnit - using assertions in external object

我正在开发一个将测试导出为 PHP/PHPUnit 的工具,但我遇到了一个小问题。简而言之,测试脚本仅包含对 actionwords 对象的调用,该对象包含测试的所有逻辑(以便从不同的测试场景中分解出不同的步骤)。 一个例子可能更清楚:

require_once('Actionwords.php');

class CoffeeMachineHiptestPublisherSampleTest extends PHPUnit_Framework_TestCase {
  public $actionwords = new Actionwords();


  public function simpleUse() {
    $this->actionwords->iStartTheCoffeeMachine();
    $this->actionwords->iTakeACoffee();
    $this->actionwords->coffeeShouldBeServed();
  }
}

coffeeShouldBeServed 方法中,我需要 运行 断言,但这是不可能的,因为 Actionwords class 没有扩展 PHPUnit_Framework_TestCase(而且我'我不确定它应该,它不是测试用例,只是一组助手)。

目前我找到的解决方案是将测试对象传递给动作词并使用断言的引用,类似那样。

class CoffeeMachineHiptestPublisherSampleTest extends PHPUnit_Framework_TestCase {
  public $actionwords;

  public function setUp() {
    $this->actionwords = new Actionwords($this);
  }
}

class Actionwords {
  var $tests;

  function __construct($tests) {
    $this->tests = $tests;
  }

  public function coffeeShouldBeServed() {
    $this->tests->assertTrue($this->sut->coffeeServed);
  }
}

它工作正常,但我觉得它不是很优雅。我不是 PHP 开发人员,所以可能会有一些更好的解决方案,感觉更 "php-ish"。

提前致谢, 文森特

断言方法是静态的,因此您可以使用 PHPUnit_Framework_Assert::assertEquals(),例如。