How do I use PHPUnit with CodeIgniter 3

浪子不回头ぞ 提交于 2019-12-12 03:16:41

问题


I have unsuccessfully tried to use PHPUnit 4.4.2 with CodeIgniter 3 .

CodeIgniter 3 is the branch under active development and it support phpunit

i don't know what is wrong with my code here .

<?php
// post_test.php
class Post_test extends CI_TestCase {
  private $ci_obj;

  public function setUp() {
    $loader = $this->ci_core_class('loader');
    $this->load = new $loader();
    $this->ci_obj = $this->ci_instance();
    $this->ci_set_core_class('model', 'CI_Model');
    $test =  $this->ci_vfs_clone('application/models/post.php');
    $this->load->model('post');
  }

  public function testGetAllPosts() {
    $posts = $this->ci_obj->post->getAll();
    $this->assertEquals(5, count($posts));
  }
}

and the model

<?php
//post.php
class Post extends CI_Model {

  public function getAll() {
    return array(
      array('title'=>'post 1','content'=>'...'),
      array('title'=>'post 2','content'=>'...'),
      array('title'=>'post 3','content'=>'...'),
      array('title'=>'post 4','content'=>'...'),
      array('title'=>'post 5','content'=>'...'),
    );
  }
}

it gives me this error

............S............................PHP Fatal error:  Call to a member function getChild() on a non-object in /path/tests/mocks/ci_testcase.php on line 241

Fatal error: Call to a member function getChild() on a non-object in /path/tests/mocks/ci_testcase.php on line 241
/path/tests 

回答1:


Add parent::setUp(); inside setUp() function.

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

Rename model name from application/models/post.php to application/models/Post.php and also fix the path of ci_vfs_clone in the test file.



来源:https://stackoverflow.com/questions/28122397/how-do-i-use-phpunit-with-codeigniter-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!