Welcome Guest, Not a member yet? Register   Sign In
ci-phpunit-test for CodeIgniter 3.0
#48

Hi, I'm trying to use your this library, it works fine with controllers, I have a problem with model testing.

As instructed in your guide, I extracted your library into application/tests
I tried with my own model test, and then created test for your Category_model, also, the model with a proper method is created, as well as table in database.
Your CategorySeeder successfully inserted data into database.
Category_model_test.php is copy/paste as from example:
PHP Code:
class Category_model_test extends TestCase
{
    public static function 
setUpBeforeClass()
    {
        
parent::setUpBeforeClass();
        
        
$CI =& get_instance();
        
$CI->load->library('Seeder');
        
$CI->seeder->call('CategorySeeder');
    }
    public function 
setUp()
    {
        
$this->resetInstance();
        
$this->CI->load->model('Category_model');
        
$this->obj $this->CI->Category_model;
    }
    public function 
test_get_category_list()
    {
        
$expected = [
            
=> 'Book',
            
=> 'CD',
            
=> 'DVD',
        ];
        
$list $this->obj->get_category_list();
        foreach (
$list as $category) {
            
$this->assertEquals($expected[$category->id], $category->name);
        }
    }



I've got the following error by PHPunit;

Code:
Fatal error: Call to a member function query() on null in /var/www/html/lipa2016/application/models/Category_model.php on line 11
/Category_model.php on line 11 is like that:
return $this->db->query("SELECT * FROM category");

Category_model.php is this:

PHP Code:
<?php
class Category_model extends CI_Model {
    
    private $category 
'category';
    
    function __construct
() {
        parent::__construct ();
    }
    
    public 
function get_category_list() {
        return $this->db->query("SELECT * FROM category");
        //$this->db->order_by ( 'name', 'asc' );
        //return $this->db->get ('category');  
    }
    



If I uncomment query like this:
$this->db->order_by ( 'name', 'asc' );
return $this->db->get ('category');
I've got error from phpunit:

Code:
Severity: Error
Message: Call to a member function order_by() on null
Filename: /var/www/html/lipa2016/application/models/Category_model.php
Line Number: 12


PHPUnit version is 5.1.3, PHP version is 5.6.14,
The same error was with PHPUnit 4.8 and php 5.4.

If you need more details about the code, please, let me know.

Regards,
Reply


Messages In This Thread
ci-phpunit-test for CodeIgniter 3.0 - by kenjis - 05-09-2015, 11:33 PM
RE: ci-phpunit-test for CodeIgniter 3.0 - by resolv_25 - 12-30-2015, 01:43 PM



Theme © iAndrew 2016 - Forum software by © MyBB