在phpunit 5.4

时间:2016-07-29 12:10:13

标签: php codeigniter phpunit ecomdev-phpunit

我试图在linux(centos 6.6)命令提示符下执行以下脚本,但我收到错误试图获取非对象的属性。我使用的是php 5.6和phpunit 5.4。 这是源文件....     

    protected function getTenants()
    {
        $this->db->select('*');
        $this->db->from('tenant');
        $this->db->where('status',1);
        $rs=$this->db->get();
        return $rs->result();
    }
    public function getInactiveTenants()
    {
        $this->db->select('*');
        $this->db->from('tenant');
        $this->db->where('status',0);
        $rs=$this->db->get();
        return $rs->result();
    }
    public function getTenant($id)
    {
        $this->db->select('*');
        $this->db->from('tenant');
        $this->db->where('tenant_id',$id);
        $rs=$this->db->get();
        return $rs->row();
    }
    public function updateTenant($options = array()){
        extract($options);
        //print_r($options);exit;
        $this->db->set ( 'mobile_num', $mobile_num );
        $this->db->set ( 'email', $email );
        $this->db->set ( 'name', $name );
        $this->db->set ( 'phone_num', $phone_num );
        $this->db->set ( 'description', $description );
        $this->db->set ( 'address', $address );
        $this->db->set ( 'city', $city );
        $this->db->set ( 'state', $state );
        $this->db->set ( 'country', $country );
        $this->db->set ( 'created_by', $this->phpsession->get('sess_user_id'));
        $this->db->where('tenant_id',$tenant_id);
        $this->db->update ( "tenant" );
        return 1;
    }

这是我用来运行以测试上述文件的文件

class Tenant_modelTest extends PHPUnit_Framework_TestCase {
    public  $CI;


    public function testGetTenants() {
        $this->CI->load->model('tenant_model');
        $posts = $this->CI->tenant_model->getTenants();
        $this->assertEquals($rs->result(), $posts);
    }

    public function testGetInactiveTenants(){
        $this->CI->load->model('tenant_model');
                $results = $this->CI->tenant_model->getInactiveTenants();
                $this->assertEquals($rs->result(), $results);
    }

    public function testGetTenant(){
        $this->CI->load->model('tenant_model');
                $results = $this->CI->tenant_model->getTenants($id);
                $this->assertEquals($rs->result(), $results);
    }

    public function testUpdateTenant(){
        $this->CI->load->model('tenant_model');
                $results = $this->CI->tenant_model->updateTenants($options = array());
                $this->assertEquals(1, $results);

    }
}

错误:

1) Tenant_modelTest::testGetTenants Trying to get property of non-object 
/var/www/html/cloudscheduler/application/tests/tenant_modelTest.php:12 
ERRORS! Tests: 4, Assertions: 0, Errors: 4

有人可以帮我这个......

1 个答案:

答案 0 :(得分:0)

您需要实例化$CI对象:

function __construct()
{
    $this->CI =& get_instance();
}