CodeIgniter Forums
Unit Testing & Mock Objects - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Unit Testing & Mock Objects (/showthread.php?tid=16079)

Pages: 1 2


Unit Testing & Mock Objects - El Forum - 02-23-2009

[eluser]Aniket[/eluser]
Hi all,
Has anybody tried writing a test case in CI using mock objects ??.
I have integrated simpletest with CI thanks to the CI wiki but the examples provided aren't sufficeint enough coz they are not dealing with controllers.
If i want to test my controller den should i directly create a mock object of my controller or is there any other approach.


Unit Testing & Mock Objects - El Forum - 02-24-2009

[eluser]Aniket[/eluser]
It seems nobody has tried it out yet...anyways i am able to test my model using SimpleTest but without using Mock object. Will try to find a way to do it with mock object too...and will post soon.


Unit Testing & Mock Objects - El Forum - 02-24-2009

[eluser]Aniket[/eluser]
The way CI loads itself makes it difficult to create a mock object for database.. :-(.
But if anybody finds a better way post on this thread


Unit Testing & Mock Objects - El Forum - 02-24-2009

[eluser]TheFuzzy0ne[/eluser]
Apparently, your answer can be found in chapter 8 of CodeIgniter for Rapid PHP Application Development. http://blogs.linux.ie/kenguest/2008/04/10/book-review-codeigniter-for-rapid-php-application-development/


Unit Testing & Mock Objects - El Forum - 02-24-2009

[eluser]Aniket[/eluser]
hi again, i read the book but it just mentions about mock objects for database but nothing relevant is provided....it talks about test data and nothing else. I searched a lot but till now no break through. :-(


Unit Testing & Mock Objects - El Forum - 02-24-2009

[eluser]TheFuzzy0ne[/eluser]
Can you post an example of what you have so far? I was in to TDD many years ago. I've probably forgotten most of it, but who knows?


Unit Testing & Mock Objects - El Forum - 02-24-2009

[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.


Unit Testing & Mock Objects - El Forum - 02-24-2009

[eluser]TheFuzzy0ne[/eluser]
I'm so confused. That may as well be written in Russian... I guess I can't remember as much as I'd hoped. Sorry...


Unit Testing & Mock Objects - El Forum - 02-24-2009

[eluser]TheFuzzy0ne[/eluser]
Perhaps this URL may help: http://www.lastcraft.com/mock_objects_documentation.php


Unit Testing & Mock Objects - El Forum - 02-24-2009

[eluser]Aniket[/eluser]
no problem... :-)
Thanks for giving an attempt. Will try to find a solution...hope to get it soon.
Thanks again