如何将夹具导入模型中?

时间:2019-04-02 14:27:47

标签: cakephp phpunit

我最近开始用一些旧代码编写单元测试(我正在遵循cakephp Cookbook上的教程),遇到了一个问题。

一个模型使用App::import('Model', 'Model2')new Model2()ClassRegistry::init('Model2')导入另一个模型。我为这两个模型制作了夹具,但是Model2仍使用默认模型。

有人可以给我一些如何处理的指示吗?

我一直在网上寻找一些示例,但是显然phpunit不再使用Cakephp2了。

模型

App::uses('Credit', 'Model');

class Credit extends AppModel {

  public function transfer_credits($from_comp, $from_user, $to_comp, $to_user, $credits) {
    App::import('Model', 'CreditTransfer');
    $CreditTransfer = ClassRegistry::init('CreditTransfer');
    $CreditTransfer->save($data);
  }

}

测试

class CreditTest extends CakeTestCase {

  public $fixures = array('app.credit','app.credit_transfer');

  public function setUp() {
    parent::setUp();

    $this->Credit = ClassRegistry::init('Credit');
    $this->Credit->useDbConfig = 'test';
    $this->CreditTransfer = ClassRegistry::init('CreditTransfer');
    $this->CreditTransfer->useDbConfig = 'test';
  }

  public function testTransferCreditsValid() {
    $credit_tranfers_before = $this->CreditTransfer->find('count');
    $result = $this->Credit->transfer_credits(1, 1, 2, 2, 5);
    $credit_tranfers_after = $this->CreditTransfer->find('count');

    $this->assertEqual($credit_tranfers_before + 1, $credit_tranfers_after);
  }

}

因此,结论是:Model1导入Model2Model2进入实际数据库,而不使用夹具。

0 个答案:

没有答案