Welcome Guest, Not a member yet? Register   Sign In
Unit Testing & Mock Objects
#7

[eluser]Aniket[/eluser]
i am developing a demo forum application.
I have following models : ForumTopic, ForumComment....
I want to unit test ForumComment.
Following is my ForumComment model:
Code:
<?php
class ForumComment extends Model
{
    var $commentid = null;
    var $topicid = null;
    var $commenttext = null;
    var $userid = null;
    var $commentdate = null;
    function ForumComment()
    {
        parent::Model();
    }
    
    
    function getComments($topicid){
        $this->db->where('tpid',$topicid);
        $data = $this->db->get('comment');
        return $data;
    }
/**
*  Method to persist data into database
*/  
    function save()
    {
        $dbarray= array('commtext'=>$this->commenttext,
                        'commdate'=>$this->commentdate,
                        'userid'=>$this->userid,
                        'tpid'=>$this->topicid
                        );
        $this->db->insert('comment',$dbarray);
    }
}

Following is my unit test case which i was able to create ..its my first unit test case.
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class ForumCommentModelTest extends UnitTestCase{
    var $CI;
    function __construct(){
    $this->UnitTestCase('Testing Login Functionality');
    }
    function setUp(){
    $this->CI = &get;_instance();  
    $this->CI->load->model('ForumComment','forum');
    }
    function tearDown(){}

    function testForumComment_getComments(){
    $this->CI->forum->ForumComment();
    $result =  $this->CI->forum->getComments(1);
    $result=$result->num_rows();
    $this->assertEqual($result,2);
    }
}
?>
Here i check for no of rows returned. In my case its 2. So the test case passes.
But i have auto loaded the database for the test case to execute. But i want to mock my database but i cant figure out the way to create one.


Messages In This Thread
Unit Testing & Mock Objects - by El Forum - 02-23-2009, 11:10 PM
Unit Testing & Mock Objects - by El Forum - 02-24-2009, 03:56 AM
Unit Testing & Mock Objects - by El Forum - 02-24-2009, 05:34 AM
Unit Testing & Mock Objects - by El Forum - 02-24-2009, 06:22 AM
Unit Testing & Mock Objects - by El Forum - 02-24-2009, 06:25 AM
Unit Testing & Mock Objects - by El Forum - 02-24-2009, 06:37 AM
Unit Testing & Mock Objects - by El Forum - 02-24-2009, 06:50 AM
Unit Testing & Mock Objects - by El Forum - 02-24-2009, 06:55 AM
Unit Testing & Mock Objects - by El Forum - 02-24-2009, 06:56 AM
Unit Testing & Mock Objects - by El Forum - 02-24-2009, 06:57 AM
Unit Testing & Mock Objects - by El Forum - 02-24-2009, 07:06 AM
Unit Testing & Mock Objects - by El Forum - 02-27-2009, 05:27 AM
Unit Testing & Mock Objects - by El Forum - 04-23-2009, 10:48 PM
Unit Testing & Mock Objects - by El Forum - 04-24-2009, 03:14 PM
Unit Testing & Mock Objects - by El Forum - 06-16-2009, 04:44 PM
Unit Testing & Mock Objects - by El Forum - 06-05-2013, 08:31 AM



Theme © iAndrew 2016 - Forum software by © MyBB