0

我正在尝试在 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

有人可以帮我解决这个问题吗...

4

1 回答 1

1

您需要实例化$CI对象:

function __construct()
{
    $this->CI =& get_instance();
}
于 2016-07-29T13:08:51.407 回答