【问题标题】:How do I use PHPUnit with CodeIgniter 3如何在 CodeIgniter 3 中使用 PHPUnit
【发布时间】:2015-01-24 04:47:08
【问题描述】:

我尝试将PHPUnit 4.4.2CodeIgniter 3 一起使用,但未成功。

CodeIgniter 3 是正在积极开发的分支,它支持 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 

【问题讨论】:

  • 你的 PHPUnit 版本是多少?
  • @KanishkaPanamaldeniya PHPUnit 4.4.2

标签: php codeigniter phpunit


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2015-12-01
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2020-08-01
    相关资源
    最近更新 更多