问题
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