Welcome Guest, Not a member yet? Register   Sign In
basic unit testing help
#1

[eluser]dadamssg[/eluser]
can anyone help me understand the basics of unit testing? I have set up this test controller to do the testing on my model with the one function, just to start understanding. So right now the test is returning that it passes, great. Would that be the only test i need to write for that function since it only has the possibility of returning true or false...just check is_bool? And obviously those inputs i have made when i made referencing the model function are inputs i EXPECT to see(strings not arrays, or integers or a bool value, etc). Is that how you do unit testing? just write simple tests that take what you expect to be inputted, in this case an email and password(both strings) and test what you want to outputted(boolean, arrays, integers, etc.)? If that's the case can you give me a scenario in which this simple test would benefit me? i'm having a hard time envisioning a scenario in which a unit test would come in handy



Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends CI_Controller {

public function __construct()
   {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library(array('session','form_validation'));
        $this->load->database();
        $this->load->model(array('account_model'));
        
    }
    
public function index()
    {
        $this->load->library('unit_test');
        
        
        $passmatch = $this->account_model->password_match('[email protected]', md5("blahblah"));
        
        $this->unit->run($passmatch, 'is_bool', "Passowrd Match", 'checks to see if the function returns true or false');
        echo $this->unit->report();
    }

}
?>

and my model
Code:
<?php
class Account_model extends CI_Model {

    function __construct()
        {
            // Call the Model constructor
            parent::__construct();
        }
    
    function password_match($username, $password)
        {            
        $sql = "SELECT * FROM Accounts WHERE email = ? AND password = ?";
        $query = $this->db->query($sql, array($username, $password));
        $numrows = $query->num_rows();
        if($numrows == 1)
        {return TRUE;}else{return FALSE;}        
        }
#2

[eluser]dadamssg[/eluser]
now i've learned that using the real database is bad practice, wtf. I'm suppose to use "mock" data but how the hell do you incorporate that into your actual functions?




Theme © iAndrew 2016 - Forum software by © MyBB