我如何在CodeIgniter 3中使用PHPUnit [英] How do I use PHPUnit with CodeIgniter 3

查看:109
本文介绍了我如何在CodeIgniter 3中使用PHPUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将 PHPUnit 4.4.2

CodeIgniter 3是正在积极开发中的分支,它支持phpunit

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(); ...

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

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

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.

这篇关于我如何在CodeIgniter 3中使用PHPUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆