Welcome Guest, Not a member yet? Register   Sign In
What to unit test?
#1

[eluser]chobo[/eluser]
I've never unit tested in an MVC framework so I'm not sure which area(s) (model, controller, view) should be tested. Could someone explain how they test their projects? I'm using simpletest, but I'm not sure how to test the controller with it. Also, where do you guys store you test files and how do you run them (from the controller folder?). Thanks in advance.
#2

[eluser]chobo[/eluser]
I got Simpletest to work, but I'm having trouble setting a session variable in a test for testing a function. When I try to set a session variable I get the following error.

Code:
Unexpected PHP error [Cannot modify header information - headers already sent] severity [E_WARNING] in [S:\Site\CodeIgniter_1.5.4\system\libraries\Session.php line 282]


Test File Code:

Code:
<?php

// test controller
require_once APPPATH . 'libraries/SimpleAuth.php';

// When run as a whole the test cases in the test suite must have unique names
class AuthTest extends UnitTestCase
{
    
    public function __construct()
    {
        parent::__construct();
        $this->UnitTestCase("Authentication Tests");    
    }
        
    /*
    *    Test is CI object is being referenced
    *
    */
    public function testCIObject()
    {
        $auth = new SimpleAuth();
        $this->assertNotNull($auth->obj, 'CI Global object (obj) is null');
    }
    
    
    /*
    *    Tests the login check
    *
    */
    public function testLoginCheck()
    {
        $auth = new SimpleAuth();

        $auth->obj->session->set_userdata('logged_in', true);  // Throws error on this line
        $this->assertEqual($auth->isLoggedIn(), true);
    }        
}
?>


Part of SimpleAuth library I am testing
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


class SimpleAuth {
    
    
    public $obj;
        
    /*
    *    Get global CI instance
    *
    */
    public function __construct()
    {
        $this->obj =& get_instance();        
    }
    
    
    //-------------------------------------- Login methods -----------------------/
    
    /*
    *    Checks to see if user is logged in
    *
    *    @return boolean
    */
    public function isLoggedIn()
    {
  
        if ($this->obj->session) {
            if ($this->obj->session->userdata('logged_in')) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
#3

[eluser]chobo[/eluser]
I figured out the problem... When I switched back to using normal session variables ($_SESSION) it works. From now on I think i'll steer clear of these built in libraries and helpers, they seem to be more trouble then they are worth (except for validation library) that one rocks.
#4

[eluser]ACSparks[/eluser]
Chobo, where does your "Test File Code" reside in relation to your Code Igniter files? Inside or outside of the framework?




Theme © iAndrew 2016 - Forum software by © MyBB