我如何在CodeIgniter 3中使用PHPUnit [英] How do I use PHPUnit with CodeIgniter 3
问题描述
我尝试将 PHPUnit 4.4.2 与
CodeIgniter 3是正在积极开发中的分支,它支持phpunit
CodeIgniter 3 is the branch under active development and it support phpunit 我不知道这里的代码有什么问题. 和模型 它给了我这个错误
在 将模型名称从 Rename model name from 这篇关于我如何在CodeIgniter 3中使用PHPUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
<?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();
...
public function setUp() {
parent::setUp();
...
application/models/post.php
重命名为application/models/Post.php
,并在测试文件中修复ci_vfs_clone
的路径.application/models/post.php
to application/models/Post.php
and also fix the path of ci_vfs_clone
in the test file.