我如何在 CodeIgniter 3 中使用 PHPUnit

How do I use PHPUnit with CodeIgniter 3

我尝试使用 PHPUnit 4.4.2 with CodeIgniter 3 没有成功。

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

我不知道我这里的代码有什么问题。

<?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));
  }
}

和模型

<?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'=>'...'),
    );
  }
}

它给我这个错误

............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 

setUp() 函数中添加 parent::setUp();

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

将模型名称从 application/models/post.php 重命名为 application/models/Post.php 并修复测试文件中 ci_vfs_clone 的路径。