Yii模块的单元测试

时间:2014-03-31 10:13:11

标签: php unit-testing yii

我希望单独对Yii模块进行单元测试。我已成功配置它。但是当我运行模块控制器(LeadController)的测试类后发生错误。

PHPUnit 3.7.31 by Sebastian Bergmann.
  

从中读取配置   D:\ xampp \ htdocs \ AdlugeCore \ protected \ modules \ lead \ tests \ phpunit.xml

     

电子

     

时间:153毫秒,内存:3.00Mb

     

有1个错误:

     

1)LeadTest :: testActionAdd尝试获取非对象的属性

     

d:\ XAMPP \ htdocs中\ AdlugeCore \保护\模块\铅\控制器\ LeadController.php   :57   d:\ XAMPP \ htdocs中\ AdlugeCore \保护\模块\导致\测试\单元\ LeadTest.php:15

     

FAILURES!测试:1,断言:0,错误:1。

以下是我的测试类:

Yii::import('application.protected.modules.lead.controllers.*');
Yii::import('application.protected.modules.lead.components.*');

class LeadTest extends CTestCase {

    public $obj;

    public function setUp() {
        $this->obj = new LeadController(rand());
        $this->obj->init();
    } 

    public function testActionAdd() {
        $this->obj->actionAdd();
        $this->assertTrue(TRUE);
    }
    public function tearDown() {
        unset($this->obj);
    }
}

在LeadController.php中

class LeadController extends Controller {

    public $lead;

    public function init() {

        $this->lead = new LeadManager();
    }

    public function actionAdd($param = array()) {

        $result = $this->lead->AddProcess($param);
        return $result;
    }
}

请帮我解决这个问题...

1 个答案:

答案 0 :(得分:0)

是的,问题是在进行单元测试时从不调用方法init()。 如果你在__construct()方法中放入那段代码,那你就没事了。