当我在命令提示符下运行phpunit测试用例时,获取'LogicException:没有这样的属性:session'

时间:2016-08-22 09:16:23

标签: php phpunit codeigniter-3 ecomdev-phpunit

我试图运行一个控制器的phpunit测试用例。但我正在说那个

1)DefinerControllerTestCase :: test_insertImageDetails LogicException:没有这样的属性:session

/var/www/html/application/tests/_ci_phpunit_test/CIPHPUnitTestCase.php:44 /var/www/html/application/tests/controllers/DefinerControllerTestCase.php:33

我的codegeniter verrsion是3.1,php单元版本是5.5.0 这是我的控制器代码

class Definer extends CI_Controller {

    function __construct() 
    {
        parent::__construct();
        date_default_timezone_set('Asia/Kolkata');
        $this->load->model("definer_model");
    }
    public function index()
    {

    }
    public function insertImageDetails()
    {
        $details['newImageName']=$this->session->userdata('newImageName');
        $data['imageOSName']=$this->session->userdata('imageOSName');
        $data['ImageReceipes']=$this->session->userdata("ImageReceipes");
        $data['imageOthers']=$this->session->userdata('imageOthers');
        $details['requirement']=json_encode($data);
        $details['defined_by_user']=$this->session->userdata('user_id');
        $details['defined_by_role']=$this->session->userdata('user_role');
        $details['date']=date('Y-m-d H:i:s');
        $details['status']='Y';
        $this->definer_model->addImageDetails($details);
        redirect(site_url('Definer/AddNewImage')); 

    }

}

测试用例代码在这里

class DefinerControllerTestCase extends TestCase
{
  public function __construct(){
  parent::__construct();

 $this->definer = new Definer();
 }

 public function test_insertImageDetails()
 {
 $_SESSION["newImageName"] ='TestSample';
     $this->session->set_userdata('newImageName', 'TestSample');
     $this->session->set_userdata('imageOSName', 'TestSample');
     $this->session->set_userdata('ImageReceipes', 'TestSample');
     $this->session->set_userdata('user_id', 'TestSample');
     $this->session->set_userdata('user_role', 'TestSample');
     $this->session->set_userdata('imageOthers', 'TestSample');
     $output = $this->request('GET', ['Definer', 'insertImageDetails']);
  if($output != "")
  {
   $this->assertContains('<title>Golden Image Managament System Dashboard</title>', $output);
  }
 } 
} 

请帮助我......

1 个答案:

答案 0 :(得分:0)

我通过以下代码解决了这个问题。

public function test_insertImageDetails()
{
$this->request->setCallable(
function ($CI) {
$CI->session->newImageName = 'TestSample';
$CI->session->imageOSName = 'TestSample';
$CI->session->ImageReceipes = 'TestSample';
$CI->session->user_id = 'TestSample';
$CI->session->user_role = 'TestSample';
$CI->session->imageOthers = 'TestSample';
}
);

$this->request('GET', ['Definer', 'insertImageDetails']);
$this->assertTrue(true);

}