phpunit:改进 PHPUnit 对 BDD 的支持

  PHPUnit 3.3 测试版已经支持 BDD 了但有两个主要缺点:

  1、测试代码和 Story 文本差距很大不容易编写和阅读;

  2、如果有失败测试没法显示出到底是哪个测试没有通过

  经过简单扩展并且利用 PHP 5.3 对闭包支持可以采用下面格式编写 BDD 测试代码:

/**
* 测试从账户中取现
*/
AccountHolderWithdrawsCashSpec extends PHPUnit_Extensions_Story_Runner
{
  /**
   * @scenario
   * 场景 1: 帐户有足够资金
   */
  function AccountHasSufficientFunds
  {
    // GIVEN
    $this->given('帐户余额为 100', function (& $world) 
      {
        // 由于 Account 对象必须属于个 AccountHolder(帐户持有人)
        // 因此需要构造个 AccountHolder 对象
        $account_holder = AccountHolder;
        $account_holder->name = 'tester';
        // 创建个 Account 对象并设置余额为 $arguments[0]
        $world['account'] = Account($account_holder);
        $world['account']->balance = 100;
      })
      ->and('有效银行卡', function (& $world)
      {
        $world['card'] = CreditCard($world['account']);
        $world['card']->valid = true;
      })
      ->and('提款机有足够现金', function (& $world)
      {
        // 确保 ATM 余额大于帐户余额
        $world['atm'] = ATM;
        $world['atm']->balance = $world['account']->balance + 1;
 
      })
 
    // WHEN
    ->when('帐户持有人要求取款 20', function (& $world)
      {
        $world['account']->drawingByATM($world['atm'], $world['card'], 20);
      })
 
    // THEN
    ->then('提款机应该分发 20', function (& $world, $action)
      {
        $this->assertEquals(20, $world['atm']->last_dispense, $action);
      })
      ->and('帐户余额应该为 80', function (& $world, $action)
      {
        $this->assertEquals(80, $world['account']->balance, $action);
      })
      ->and('应该退还银行卡', function (& $world, $action)
      {
        $this->assertTrue($world['card']->isCheckedOut, $action);
      });
  }
}


  看上去应该好看多了呵呵

Tags:  改进phpunitbdd 持续改进 phpunit2 phpunit

延伸阅读

最新评论

发表评论